File:  [NetBSD Developer Wiki] / wikisrc / tutorials / per_file_build_options_override.mdwn
Revision 1.2: download - view: text, annotated - select for diffs
Sun Feb 5 07:14:36 2012 UTC (2 years, 2 months ago) by schmonz
Branches: MAIN
CVS tags: HEAD
dos2unix

**Contents**

[[!toc levels=3]]

#  Introduction 

Build variables like CFLAGS, LDFLAGS, CPUFLAGS etc. are used to override default build options and to take advantage of special compiler options available on some target hardware platforms. The build variables are usually supplied through shell variables, make options or mk.conf, so original build infrastructure can remain clean of hacks. Most build options can be applied globally to the whole NetBSD userspace or to some library, program or source file for example, but sometimes the special options need a few exception within the source tree. This is where per-source-file build options override comes in handy. 

#  Per source file build option override 

For example the CPUFLAGS build variable can be changed to depend on the source file being compiled: 
    
    CPUFLAGS_source_file_name_without_white_space_and_extension=-mnormal_arch \
    DEF_CPUFLAGS=-mspecial_arch \
    CPUFLAGS='${CPUFLAGS_${.IMPSRC:T:R}:U${DEF_CPUFLAGS}}' \
    nbmake
    

When the BSD make builds a C file (or the build command uses the CPUFLAGS variable), a check is made if a variable named CPUFLAGS_source_file_name_without_white_space_and_extension exists. If this variable exists, its contents is used as CPUFLAGS, and if it does not exist, DEF_CPUFLAGS is used instead. See make manual for more variable name filters, regexp possibilities and other predifined make variables, which may all come in handy. 

#  Example: libc cross-build for ARM/thumb, except for atomic_init_testset.c 
    
    $ cd lib/libc
    $ CPUFLAGS_atomic_init_testset=-mthumb-interwork \
    > DEF_CPUFLAGS='-mthumb -mthumb-interwork' \
    > CPUFLAGS='${CPUFLAGS_${.IMPSRC:T:R}:U${DEF_CPUFLAGS}}' \
    > $TOOLDIR/bin/nbmake-evbarm
    

#  Refences 

  * man 1 make 
  * [Per-source CFLAGS, discussion on freebsd-arch](http://lists.freebsd.org/pipermail/freebsd-arch/2003-June/000906.html)
  * [NetBSD Guide: Chapter 30. Crosscompiling NetBSD with build.sh](http://www.netbsd.org/docs/guide/en/chap-build.html)
  * share/mk/bsd.README 
  * BUILDING 


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