1 # Copyright 2003 Christopher Currie
2 # Copyright 2006 Dave Abrahams
3 # Copyright 2003, 2004, 2005, 2006 Vladimir Prus
4 # Copyright 2005-2007 Mat Marcus
5 # Copyright 2005-2007 Adobe Systems Incorporated
6 # Copyright 2007-2010 Rene Rivera
7 # Distributed under the Boost Software License, Version 1.0.
8 # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
10 # Please see http://article.gmane.org/gmane.comp.lib.boost.build/3389/
11 # for explanation why it's a separate toolset.
13 import feature : feature ;
14 import toolset : flags ;
18 import path : basename ;
25 feature framework : : free ;
27 ## The MacOSX version to compile for, which maps to the SDK to use (sysroot).
28 feature macosx-version : : propagated link-incompatible symmetric optional ;
30 ## The minimal MacOSX version to target.
31 feature macosx-version-min : : propagated optional ;
33 ## A dependency, that is forced to be included in the link.
34 feature force-load : : free dependency incidental ;
36 #############################################################################
40 if [ MATCH (--debug-configuration) : [ modules.peek : ARGV ] ]
42 .debug-configuration = true ;
45 feature.extend toolset : darwin ;
47 toolset.inherit-generators darwin : gcc : gcc.mingw.link gcc.mingw.link.dll ;
49 generators.override darwin.prebuilt : builtin.prebuilt ;
50 generators.override darwin.searched-lib-generator : searched-lib-generator ;
52 # Override default do-nothing generators.
53 generators.override darwin.compile.c.pch : pch.default-c-pch-generator ;
54 generators.override darwin.compile.c++.pch : pch.default-cpp-pch-generator ;
56 type.set-generated-target-suffix PCH : <toolset>darwin : gch ;
58 toolset.inherit-rules darwin : gcc : localize ;
59 toolset.inherit-flags darwin : gcc
60 : <runtime-link>static
61 <architecture>arm/<address-model>32
62 <architecture>arm/<address-model>64
63 <architecture>arm/<instruction-set>
64 <architecture>x86/<address-model>32
65 <architecture>x86/<address-model>64
66 <architecture>x86/<instruction-set>
67 <architecture>power/<address-model>32
68 <architecture>power/<address-model>64
69 <architecture>power/<instruction-set>
75 # Platform root path. The common autodetection will set this to
76 # "/Developer". And when a command is given it will be set to
77 # the corresponding "*.platform/Developer" directory.
79 rule init ( version ? : command * : options * : requirement * )
81 # First time around, figure what is host OSX version
82 if ! $(.host-osx-version)
84 .host-osx-version = [ MATCH "^([0-9.]+)"
85 : [ SHELL "/usr/bin/sw_vers -productVersion" ] ] ;
86 if $(.debug-configuration)
88 ECHO notice: OSX version on this machine is $(.host-osx-version) ;
92 # - The root directory of the tool install.
93 local root = [ feature.get-values <root> : $(options) ] ;
95 # - The bin directory where to find the commands to execute.
98 # - The configured compile driver command.
99 local command = [ common.get-invocation-command darwin : g++ : $(command) ] ;
101 # The version as reported by the compiler
104 # - Autodetect the root and bin dir if not given.
107 bin ?= [ common.get-absolute-tool-path $(command[1]) ] ;
108 if $(bin) = "/usr/bin"
120 # - Autodetect the version if not given.
123 # - The 'command' variable can have multiple elements. When calling
124 # the SHELL builtin we need a single string.
125 local command-string = $(command:J=" ") ;
126 real-version = [ MATCH "^([0-9.]+)"
127 : [ SHELL "$(command-string) -dumpversion" ] ] ;
128 version ?= $(real-version) ;
131 .real-version.$(version) = $(real-version) ;
133 # - Define the condition for this toolset instance.
135 [ common.check-init-parameters darwin $(requirement) : version $(version) ] ;
137 # - Set the toolset generic common options.
138 common.handle-options darwin : $(condition) : $(command) : $(options) ;
140 # - GCC 4.0 and higher in Darwin does not have -fcoalesce-templates.
141 if $(real-version) < "4.0.0"
143 flags darwin.compile.c++ OPTIONS $(condition) : -fcoalesce-templates ;
145 # - GCC 4.2 and higher in Darwin does not have -Wno-long-double.
146 if $(real-version) < "4.2.0"
148 flags darwin.compile OPTIONS $(condition) : -Wno-long-double ;
150 # - GCC on Darwin with -pedantic, suppress unsupported long long warning
151 flags darwin.compile OPTIONS $(condition)/<warnings>all : -Wno-long-long ;
153 # - GCC on El Capitan (10.11) does not suport -finline-functions
154 if "10.11.0" <= $(.host-osx-version)
156 flags darwin.compile OPTIONS $(condition)/<inlining>full : -Wno-inline ;
159 # - Set the link flags common with the GCC toolset.
160 gcc.init-link-flags darwin darwin $(condition) ;
162 # - The symbol strip program.
164 if <striper> in $(options)
166 # We can turn off strip by specifying it as empty. In which
167 # case we switch to using the linker to do the strip.
168 flags darwin.link.dll OPTIONS
169 $(condition)/<main-target-type>LIB/<link>shared/<address-model>32/<strip>on : -Wl,-x ;
170 flags darwin.link.dll OPTIONS
171 $(condition)/<main-target-type>LIB/<link>shared/<address-model>/<strip>on : -Wl,-x ;
172 flags darwin.link OPTIONS
173 $(condition)/<main-target-type>EXE/<address-model>32/<strip>on : -s ;
174 flags darwin.link OPTIONS
175 $(condition)/<main-target-type>EXE/<address-model>/<strip>on : -s ;
179 # Otherwise we need to find a strip program to use. And hence
180 # also tell the link action that we need to use a strip
182 flags darwin.link NEED_STRIP $(condition)/<strip>on : "" ;
184 [ common.get-invocation-command darwin
185 : strip : [ feature.get-values <striper> : $(options) ] : $(bin) : search-path ] ;
186 flags darwin.link .STRIP $(condition) : $(strip[1]) ;
187 if $(.debug-configuration)
189 ECHO notice: using strip for $(condition) at $(strip[1]) ;
193 # - The archive builder (libtool is the default as creating
194 # archives in darwin is complicated.
196 [ common.get-invocation-command darwin
197 : libtool : [ feature.get-values <archiver> : $(options) ] : $(bin) : search-path ] ;
198 flags darwin.archive .LIBTOOL $(condition) : $(archiver[1]) ;
199 if $(.debug-configuration)
201 ECHO notice: using archiver for $(condition) at $(archiver[1]) ;
204 # - Initialize the SDKs available in the root for this tool.
205 local sdks = [ init-available-sdk-versions $(condition) : $(root) ] ;
208 #~ ECHO --- bin :: $(bin) ;
209 #~ ECHO --- root :: $(root) ;
210 #~ ECHO --- version :: $(version) ;
211 #~ ECHO --- condition :: $(condition) ;
212 #~ ECHO --- strip :: $(strip) ;
213 #~ ECHO --- archiver :: $(archiver) ;
214 #~ ECHO --- sdks :: $(sdks) ;
219 # Add and set options for a discovered SDK version.
220 local rule init-sdk ( condition * : root ? : version + : version-feature ? )
222 local rule version-to-feature ( version + )
228 return $(version[1])-$(version[2-]:J=.) ;
232 return $(version[1])-$(version[2-]:J=.) ;
236 return $(version[2-]:J=.) ;
240 return $(version:J=.) ;
245 if $(version-feature)
247 if $(.debug-configuration)
249 ECHO notice: available sdk for $(condition)/<macosx-version>$(version-feature) at $(root) ;
252 # Add the version to the features for specifying them.
253 if ! $(version-feature) in [ feature.values macosx-version ]
255 feature.extend macosx-version : $(version-feature) ;
257 if ! $(version-feature) in [ feature.values macosx-version-min ]
259 feature.extend macosx-version-min : $(version-feature) ;
262 # Set the flags the version needs to compile with, first
264 flags darwin.compile OPTIONS $(condition)/<macosx-version>$(version-feature)
265 : -isysroot $(root) ;
266 flags darwin.link OPTIONS $(condition)/<macosx-version>$(version-feature)
267 : -isysroot $(root) ;
269 # Then device variation options.
274 local N = $(version[2]) ;
275 if ! $(version[3]) { N += 00 ; }
276 else if [ regex.match (..) : $(version[3]) ] { N += $(version[3]) ; }
277 else { N += 0$(version[3]) ; }
278 if ! $(version[4]) { N += 00 ; }
279 else if [ regex.match (..) : $(version[4]) ] { N += $(version[4]) ; }
280 else { N += 0$(version[4]) ; }
282 flags darwin.compile OPTIONS <macosx-version-min>$(version-feature)
283 : -D__IPHONE_OS_VERSION_MIN_REQUIRED=$(N) ;
284 flags darwin.link OPTIONS <macosx-version-min>$(version-feature)
285 : -D__IPHONE_OS_VERSION_MIN_REQUIRED=$(N) ;
290 flags darwin.compile OPTIONS <macosx-version-min>$(version-feature)
291 : -mtvos-version-min=$(version[2-]:J=.) ;
292 flags darwin.link OPTIONS <macosx-version-min>$(version-feature)
293 : -mtvos-version-min=$(version[2-]:J=.) ;
298 local N = $(version[2]) ;
299 if ! $(version[3]) { N += 00 ; }
300 else if [ regex.match (..) : $(version[3]) ] { N += $(version[3]) ; }
301 else { N += 0$(version[3]) ; }
302 if ! $(version[4]) { N += 00 ; }
303 else if [ regex.match (..) : $(version[4]) ] { N += $(version[4]) ; }
304 else { N += 0$(version[4]) ; }
306 flags darwin.compile OPTIONS <macosx-version-min>$(version-feature)
307 : -D__IPHONE_OS_VERSION_MIN_REQUIRED=$(N) ;
308 flags darwin.link OPTIONS <macosx-version-min>$(version-feature)
309 : -D__IPHONE_OS_VERSION_MIN_REQUIRED=$(N) ;
314 flags darwin.compile OPTIONS <macosx-version-min>$(version-feature)
315 : -miphoneos-version-min=$(version[2-]:J=.) ;
316 flags darwin.link OPTIONS <macosx-version-min>$(version-feature)
317 : -miphoneos-version-min=$(version[2-]:J=.) ;
322 flags darwin.compile OPTIONS <macosx-version-min>$(version-feature)
323 : -mmacosx-version-min=$(version[2-]:J=.) ;
324 flags darwin.link OPTIONS <macosx-version-min>$(version-feature)
325 : -mmacosx-version-min=$(version[2-]:J=.) ;
331 # We have a minor version of an SDK. We want to set up
332 # previous minor versions, plus the current minor version.
333 # So we recurse to set up the previous minor versions, up to
334 # the current version.
335 local minor-minus-1 = [ CALC $(version[3]) - 1 ] ;
337 [ init-sdk $(condition) : $(root)
338 : $(version[1-2]) $(minor-minus-1) : [ version-to-feature $(version[1-2]) $(minor-minus-1) ] ]
343 return $(version-feature) ;
346 else if $(version[4])
348 # We have a patch version of an SDK. We want to set up
349 # both the specific patch version, and the minor version.
350 # So we recurse to set up the patch version. Plus the minor version.
352 [ init-sdk $(condition) : $(root)
353 : $(version[1-3]) : [ version-to-feature $(version[1-3]) ] ]
354 [ init-sdk $(condition) : $(root)
355 : $(version) : [ version-to-feature $(version) ] ] ;
359 # Yes, this is intentionally recursive.
361 [ init-sdk $(condition) : $(root)
362 : $(version) : [ version-to-feature $(version) ] ] ;
366 # Determine the MacOSX SDK versions installed and their locations.
367 local rule init-available-sdk-versions ( condition * : root ? )
370 local sdks-root = $(root)/SDKs ;
371 local sdks = [ GLOB $(sdks-root) : MacOSX*.sdk AppleTVOS*.sdk AppleTVSimulator*.sdk iPhoneOS*.sdk iPhoneSimulator*.sdk ] ;
373 for local sdk in $(sdks)
375 local sdk-match = [ MATCH ([^0-9]+)([0-9]+)[.]([0-9x]+)[.]?([0-9x]+)? : $(sdk:D=) ] ;
376 local sdk-platform = $(sdk-match[1]:L) ;
377 local sdk-version = $(sdk-match[2-]) ;
380 switch $(sdk-platform)
384 sdk-version = mac $(sdk-version) ;
388 sdk-version = appletv $(sdk-version) ;
390 case appletvsimulator :
392 sdk-version = appletvsim $(sdk-version) ;
396 sdk-version = iphone $(sdk-version) ;
398 case iphonesimulator :
400 sdk-version = iphonesim $(sdk-version) ;
404 sdk-version = $(sdk-version:J=-) ;
407 result += [ init-sdk $(condition) : $(sdk) : $(sdk-version) ] ;
414 flags darwin.compile OPTIONS <flags> ;
416 # The following adds objective-c support to darwin.
417 # Thanks to http://thread.gmane.org/gmane.comp.lib.boost.build/13759
419 generators.register-c-compiler darwin.compile.m : OBJECTIVE_C : OBJ : <toolset>darwin ;
420 generators.register-c-compiler darwin.compile.mm : OBJECTIVE_CPP : OBJ : <toolset>darwin ;
422 rule setup-address-model ( targets * : sources * : properties * )
424 local ps = [ property-set.create $(properties) ] ;
425 local arch = [ $(ps).get <architecture> ] ;
426 local instruction-set = [ $(ps).get <instruction-set> ] ;
427 local address-model = [ $(ps).get <address-model> ] ;
428 local osx-version = [ $(ps).get <macosx-version> ] ;
429 local gcc-version = [ $(ps).get <toolset-darwin:version> ] ;
430 gcc-version = $(.real-version.$(gcc-version)) ;
433 local support-ppc64 = 1 ;
435 osx-version ?= $(.host-osx-version) ;
437 switch $(osx-version)
445 if $(osx-version) && ! [ version.version-less [ regex.split $(osx-version) \\. ] : 10 6 ]
447 # When targeting 10.6:
448 # - gcc 4.2 will give a compiler errir if ppc64 compilation is requested
449 # - gcc 4.0 will compile fine, somehow, but then fail at link time
457 if $(address-model) = 32_64 {
458 if $(support-ppc64) {
459 options = -arch i386 -arch ppc -arch x86_64 -arch ppc64 ;
462 options = -arch i386 -arch ppc -arch x86_64 ;
464 } else if $(address-model) = 64 {
465 if $(support-ppc64) {
466 options = -arch x86_64 -arch ppc64 ;
468 errors.user-error "64-bit PPC compilation is not supported when targeting OSX 10.6 or later" ;
471 options = -arch i386 -arch ppc ;
477 if $(address-model) = 32_64 {
478 options = -arch i386 -arch x86_64 ;
479 } else if $(address-model) = 64 {
480 options = -arch x86_64 ;
482 options = -arch i386 ;
488 if ! $(support-ppc64)
489 && ( $(address-model) = 32_64 || $(address-model) = 64 )
491 errors.user-error "64-bit PPC compilation is not supported when targeting OSX 10.6 or later" ;
494 if $(address-model) = 32_64 {
495 options = -arch ppc -arch ppc64 ;
496 } else if $(address-model) = 64 {
497 options = -arch ppc64 ;
499 options = -arch ppc ;
505 if $(instruction-set) {
506 options = -arch$(_)$(instruction-set) ;
508 options = -arch arm ;
515 OPTIONS on $(targets) += $(options) ;
519 rule compile.m ( targets * : sources * : properties * )
521 LANG on $(<) = "-x objective-c" ;
522 gcc.set-fpic-options $(targets) : $(sources) : $(properties) ;
523 setup-address-model $(targets) : $(sources) : $(properties) ;
528 "$(CONFIG_COMMAND)" $(LANG) $(OPTIONS) $(USER_OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -c -o "$(<)" "$(>)"
531 rule compile.mm ( targets * : sources * : properties * )
533 LANG on $(<) = "-x objective-c++" ;
534 gcc.set-fpic-options $(targets) : $(sources) : $(properties) ;
535 setup-address-model $(targets) : $(sources) : $(properties) ;
540 "$(CONFIG_COMMAND)" $(LANG) $(OPTIONS) $(USER_OPTIONS) -D$(DEFINES) -I"$(INCLUDES)" -c -o "$(<)" "$(>)"
543 # Set the max header padding to allow renaming of libs for installation.
544 flags darwin.link.dll OPTIONS : -headerpad_max_install_names ;
546 # To link the static runtime we need to link to all the core runtime libraries.
547 flags darwin.link OPTIONS <runtime-link>static
548 : -nodefaultlibs -shared-libgcc -lstdc++-static -lgcc_eh -lgcc -lSystem ;
550 # Strip as much as possible when optimizing.
551 flags darwin.link OPTIONS <optimization>speed : -Wl,-dead_strip -no_dead_strip_inits_and_terms ;
552 flags darwin.link OPTIONS <optimization>space : -Wl,-dead_strip -no_dead_strip_inits_and_terms ;
554 # Dynamic/shared linking.
555 flags darwin.compile OPTIONS <link>shared : -dynamic ;
558 flags darwin.compile OPTIONS : -gdwarf-2 -fexceptions ;
559 #~ flags darwin.link OPTIONS : -fexceptions ;
561 # Add the framework names to use.
562 flags darwin.link FRAMEWORK <framework> ;
565 flags darwin.link FORCE_LOAD <force-load> ;
567 # This is flag is useful for debugging the link step
568 # uncomment to see what libtool is doing under the hood
569 #~ flags darwin.link.dll OPTIONS : -Wl,-v ;
571 # set up the -F option to include the paths to any frameworks used.
572 local rule prepare-framework-path ( target + )
574 # The -framework option only takes basename of the framework.
575 # The -F option specifies the directories where a framework
576 # is searched for. So, if we find <framework> feature
577 # with some path, we need to generate property -F option.
578 local framework-paths = [ on $(target) return $(FRAMEWORK:D) ] ;
580 # Be sure to generate no -F if there's no path.
581 for local framework-path in $(framework-paths)
583 if $(framework-path) != ""
585 FRAMEWORK_PATH on $(target) += -F$(framework-path) ;
590 rule link ( targets * : sources * : properties * )
592 DEPENDS $(targets) : [ on $(targets) return $(FORCE_LOAD) ] ;
593 setup-address-model $(targets) : $(sources) : $(properties) ;
594 prepare-framework-path $(<) ;
597 # Note that using strip without any options was reported to result in broken
598 # binaries, at least on OS X 10.5.5, see:
599 # http://svn.boost.org/trac/boost/ticket/2347
601 actions link bind LIBRARIES FORCE_LOAD
603 "$(CONFIG_COMMAND)" -L"$(LINKPATH)" -o "$(<)" "$(>)" -Wl,-force_load$(_)"$(FORCE_LOAD)" "$(LIBRARIES)" -l$(FINDLIBS-SA) -l$(FINDLIBS-ST) $(FRAMEWORK_PATH) -framework$(_)$(FRAMEWORK:D=:S=) $(OPTIONS) $(USER_OPTIONS)
604 $(NEED_STRIP)"$(.STRIP)" $(NEED_STRIP)-S $(NEED_STRIP)-x $(NEED_STRIP)"$(<)"
607 rule link.dll ( targets * : sources * : properties * )
609 setup-address-model $(targets) : $(sources) : $(properties) ;
610 prepare-framework-path $(<) ;
613 actions link.dll bind LIBRARIES
615 "$(CONFIG_COMMAND)" -dynamiclib -Wl,-single_module -install_name "$(<:B)$(<:S)" -L"$(LINKPATH)" -o "$(<)" "$(>)" "$(LIBRARIES)" -l$(FINDLIBS-SA) -l$(FINDLIBS-ST) $(FRAMEWORK_PATH) -framework$(_)$(FRAMEWORK:D=:S=) $(OPTIONS) $(USER_OPTIONS)
618 # We use libtool instead of ar to support universal binary linking
619 # TODO: Find a way to use the underlying tools, i.e. lipo, to do this.
620 actions piecemeal archive
622 "$(.LIBTOOL)" -static -o "$(<:T)" $(ARFLAGS) "$(>:T)"