Annotation of wikisrc/tutorials/using_ccache_with_build_sh.mdwn, revision 1.1

1.1     ! mspo        1: Google turned up nothing when I was searching for how to use ccache with NetBSD's build.sh script. My "solutions" are fairly ugly but hopefully point someone in the right direction. There may very well be better ways of doing things, but this works for me. 
        !             2: 
        !             3: (note: These steps were used for a cross-compilation of NetBSD_4.0/i386 on a FreeBSD_6.2/i386 host. The basic ideas should be fairly generic and applicable to other host/target pairs, including native NetBSD builds. The basic ideas might also help in getting build.sh to use distcc) 
        !             4: 
        !             5: The goal is to use ccache for c/c++ compiles done by build.sh (the build.sh "target" e.g. "release", "distribution", etc. should not matter, any target that compiles c/c++ code can potentially benefit from using ccache). 
        !             6: 
        !             7: This goal can be achieved by realizing 2 subgoals: 
        !             8:     
        !             9:     Objective 1) - make build.sh use ccache for HOST_CC/HOST_CXX (host compiler)
        !            10:     
        !            11:     
        !            12:     Objective 2) - make build.sh use ccache for CC/CXX (target compiler)
        !            13:     
        !            14: 
        !            15: e.g. when compiling NetBSD on a FreeBSD system, HOST_CC/HOST_CXX point to a FreeBSD compiler, which will build a NetBSD cross-compiler (CC/CXX) that runs on the host system. 
        !            16: 
        !            17:   
        !            18: For objective 1), my issue turned out to be that there are some Makefiles in the NetBSD sources that prefix some commands with ``/usr/bin/env -i``, which clears the environment. In my case, my ccache command invocation requires CCACHE_DIR/CCACHE_PATH/PATH to be set appropriately, which ``/usr/bin/env -i`` breaks. 
        !            19: 
        !            20: Fair is fair, so my workaround was simply to use the env command myself in HOST_CC/HOST_CXX: 
        !            21:     
        !            22:     export HOST_CC='env CCACHE_DIR=/whereever CCACHE_PATH=/whereever PATH=/whereever /usr/local/libexec/ccache/cc'
        !            23:     
        !            24: 
        !            25: Note: you might have quoting issues if CCACHE_DIR/CCACHE_PATH/PATH contain space characters. Such issues are beyond the scope of this document. 
        !            26: 
        !            27:   
        !            28: Objective 2) is a bit hairer. 
        !            29: 
        !            30: My first approach was simply to stick 
        !            31:     
        !            32:     CC = <ccache_stuff> ${CC}
        !            33:     CXX = <ccache_stuff> ${CXX}
        !            34:     
        !            35: 
        !            36: in a $MAKECONF file, and point build.sh at that. This fails because (near as I can tell) in XXX/src/share/mk/bsd.own.mk around line 199 (w/ NetBSD 4.0 sources) there are lines of the form: 
        !            37:     
        !            38:     if ${USETOOLS_GCC:Uyes} == "yes"                   #  {
        !            39:     CC=     ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-gcc
        !            40:     CPP=        ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-cpp
        !            41:     CXX=        ${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-c++
        !            42:     ...
        !            43:     
        !            44: 
        !            45: Even though $MAKECONF is included at the top of bsd.own.mk, these lines will override whatever $MAKECONF sets CC and friends to. 
        !            46: 
        !            47: Although I tried to avoid patching the sources at all (I build from a script, trying to automate things) I caved and added a line at line 208 in XXX/src/share/mk/bsd.own.mk. 
        !            48: 
        !            49:   
        !            50: 
        !            51:     
        !            52:     .endif  # EXTERNAL_TOOLCHAIN                        # }
        !            53:     
        !            54:     # below line was added
        !            55:     .-include "${MAKECONF}"
        !            56:     
        !            57: 
        !            58:   
        !            59: to force bsd.own.mk to use my CC/CXX values from my $MAKECONF. 
        !            60: 
        !            61: At the least, you will probably need to ensure that 
        !            62:     
        !            63:     CCACHE_PATH='"$tool_dir"'/bin'
        !            64:     PATH='"$tool_dir"'/bin:'"$PATH"
        !            65:     
        !            66: 
        !            67: are in the environment for CC/CXX. In contrast, $tool_dir/bin is NOT needed in these vars for HOST_CC/HOST_CXX. 
        !            68: 
        !            69: NOTE: $tool_dir can be specified to build.sh via ``-T <dir>
        !            70: 
        !            71:   
        !            72: Finally, when I had a $MAKECONF with: 
        !            73:     
        !            74:     CC = /usr/bin/env \
        !            75:      CCACHE_DIR=<wherever> \
        !            76:      CCACHE_PATH=<wherever> \
        !            77:      PATH=<whatever> \
        !            78:      /usr/local/bin/ccache \
        !            79:      <tool_dir>/bin/<target_arch>--netbsd<target_objformat>-gcc
        !            80:     
        !            81: 
        !            82: (sans backslashes and newlines) and thought I had won, my compile seemed to hang forever. Not sure what caused this. 
        !            83: 
        !            84: Anyhow, I ended up creating CC/CXX wrapper scripts (well, changing my build script that calls build.sh to create wrappers) 
        !            85: 
        !            86: My CC/CXX scripts are just (sans backslashes and newlines): 
        !            87:     
        !            88:     #! /bin/sh
        !            89:     # fill in with yer own paths, target arch., etc.
        !            90:     exec /usr/bin/env \
        !            91:      CCACHE_DIR=/usr/obj/ccache \
        !            92:      CCACHE_PATH=/XXX/TOOLS/bin \
        !            93:      PATH=/XXX/TOOLS/bin:<rest of $PATH> \
        !            94:      /usr/local/bin/ccache \
        !            95:      /XXX/TOOLS/bin/<arch>--netbsd<obj_format>-<gcc/c++> \
        !            96:      "$@"
        !            97:     
        !            98: 
        !            99:   
        !           100: NOTE: "$@" is important. $* will not handle multiple args containing spaces correctly. 
        !           101: 
        !           102:   
        !           103: And the $MAKECONF I (my script) passes to build.sh is simply 
        !           104:     
        !           105:     CC = /xxx/path_to_cc_wrapper
        !           106:     CXX = /xxx/path_to_cxx_wrapper
        !           107:     
        !           108: 
        !           109:   
        !           110: YMMV, but this setup works for me. If anyone knows better ways to do things, feel free to update this guide with your way of doing things. In particular, a method that does not require patching NetBSD sources at all, even if it is just a single line. 
        !           111: 

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb