1OSX_MIN_VERSION=10.14
2OSX_SDK_VERSION=10.15.6
3XCODE_VERSION=12.1
4XCODE_BUILD_ID=12A7403
5LD64_VERSION=609
6
7OSX_SDK=$(SDK_PATH)/Xcode-$(XCODE_VERSION)-$(XCODE_BUILD_ID)-extracted-SDK-with-libcxx-headers
8
9darwin_native_binutils=native_cctools
10
11ifeq ($(strip $(FORCE_USE_SYSTEM_CLANG)),)
12# FORCE_USE_SYSTEM_CLANG is empty, so we use our depends-managed, pinned clang
13# from llvm.org
14
15# Clang is a dependency of native_cctools when FORCE_USE_SYSTEM_CLANG is empty
16darwin_native_toolchain=native_cctools
17
18clang_prog=$(build_prefix)/bin/clang
19clangxx_prog=$(clang_prog)++
20
21clang_resource_dir=$(build_prefix)/lib/clang/$(native_clang_version)
22else
23# FORCE_USE_SYSTEM_CLANG is non-empty, so we use the clang from the user's
24# system
25
26darwin_native_toolchain=
27
28# We can't just use $(shell command -v clang) because GNU Make handles builtins
29# in a special way and doesn't know that `command` is a POSIX-standard builtin
30# prior to 1af314465e5dfe3e8baa839a32a72e83c04f26ef, first released in v4.2.90.
31# At the time of writing, GNU Make v4.2.1 is still being used in supported
32# distro releases.
33#
34# Source: https://lists.gnu.org/archive/html/bug-make/2017-11/msg00017.html
35clang_prog=$(shell $(SHELL) $(.SHELLFLAGS) "command -v clang")
36clangxx_prog=$(shell $(SHELL) $(.SHELLFLAGS) "command -v clang++")
37
38clang_resource_dir=$(shell clang -print-resource-dir)
39endif
40
41cctools_TOOLS=AR RANLIB STRIP NM LIBTOOL OTOOL INSTALL_NAME_TOOL
42
43# Make-only lowercase function
44lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
45
46# For well-known tools provided by cctools, make sure that their well-known
47# variable is set to the full path of the tool, just like how AC_PATH_{TOO,PROG}
48# would.
49$(foreach TOOL,$(cctools_TOOLS),$(eval darwin_$(TOOL) = $$(build_prefix)/bin/$$(host)-$(call lc,$(TOOL))))
50
51# Flag explanations:
52#
53#     -mlinker-version
54#
55#         Ensures that modern linker features are enabled. See here for more
56#         details: https://github.com/bitcoin/bitcoin/pull/19407.
57#
58#     -B$(build_prefix)/bin
59#
60#         Explicitly point to our binaries (e.g. cctools) so that they are
61#         ensured to be found and preferred over other possibilities.
62#
63#     -stdlib=libc++ -stdlib++-isystem$(OSX_SDK)/usr/include/c++/v1
64#
65#         Forces clang to use the libc++ headers from our SDK and completely
66#         forget about the libc++ headers from the standard directories
67#
68#     -Xclang -*system<path_a> \
69#     -Xclang -*system<path_b> \
70#     -Xclang -*system<path_c> ...
71#
72#         Adds path_a, path_b, and path_c to the bottom of clang's list of
73#         include search paths. This is used to explicitly specify the list of
74#         system include search paths and its ordering, rather than rely on
75#         clang's autodetection routine. This routine has been shown to:
76#             1. Fail to pickup libc++ headers in $SYSROOT/usr/include/c++/v1
77#                when clang was built manually (see: https://github.com/bitcoin/bitcoin/pull/17919#issuecomment-656785034)
78#             2. Fail to pickup C headers in $SYSROOT/usr/include when
79#                C_INCLUDE_DIRS was specified at configure time (see: https://gist.github.com/dongcarl/5cdc6990b7599e8a5bf6d2a9c70e82f9)
80#
81#         Talking directly to cc1 with -Xclang here grants us access to specify
82#         more granular categories for these system include search paths, and we
83#         can use the correct categories that these search paths would have been
84#         placed in if the autodetection routine had worked correctly. (see:
85#         https://gist.github.com/dongcarl/5cdc6990b7599e8a5bf6d2a9c70e82f9#the-treatment)
86#
87#         Furthermore, it places these search paths after any "non-Xclang"
88#         specified search paths. This prevents any additional clang options or
89#         environment variables from coming after or in between these system
90#         include search paths, as that would be wrong in general but would also
91#         break #include_next's.
92#
93darwin_CC=env -u C_INCLUDE_PATH -u CPLUS_INCLUDE_PATH \
94              -u OBJC_INCLUDE_PATH -u OBJCPLUS_INCLUDE_PATH -u CPATH \
95              -u LIBRARY_PATH \
96            $(clang_prog) --target=$(host) -mmacosx-version-min=$(OSX_MIN_VERSION) \
97              -B$(build_prefix)/bin -mlinker-version=$(LD64_VERSION) \
98              -isysroot$(OSX_SDK) \
99              -Xclang -internal-externc-isystem$(clang_resource_dir)/include \
100              -Xclang -internal-externc-isystem$(OSX_SDK)/usr/include
101darwin_CXX=env -u C_INCLUDE_PATH -u CPLUS_INCLUDE_PATH \
102               -u OBJC_INCLUDE_PATH -u OBJCPLUS_INCLUDE_PATH -u CPATH \
103               -u LIBRARY_PATH \
104             $(clangxx_prog) --target=$(host) -mmacosx-version-min=$(OSX_MIN_VERSION) \
105               -B$(build_prefix)/bin -mlinker-version=$(LD64_VERSION) \
106               -isysroot$(OSX_SDK) \
107               -stdlib=libc++ \
108               -stdlib++-isystem$(OSX_SDK)/usr/include/c++/v1 \
109               -Xclang -internal-externc-isystem$(clang_resource_dir)/include \
110               -Xclang -internal-externc-isystem$(OSX_SDK)/usr/include
111
112darwin_CFLAGS=-pipe
113darwin_CXXFLAGS=$(darwin_CFLAGS)
114
115darwin_release_CFLAGS=-O2
116darwin_release_CXXFLAGS=$(darwin_release_CFLAGS)
117
118darwin_debug_CFLAGS=-O1
119darwin_debug_CXXFLAGS=$(darwin_debug_CFLAGS)
120
121darwin_cmake_system=Darwin
122