-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
374 lines (303 loc) · 11.4 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Author: Thomas Debesse
# License: CC0 1.0
cmake_minimum_required(VERSION 3.10)
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
project(SaigoNativeClientToolchain)
execute_process(
COMMAND "${CMAKE_C_COMPILER}" -dumpmachine
OUTPUT_VARIABLE TRIPLE_HOST
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND cc -dumpmachine
OUTPUT_VARIABLE TRIPLE_BUILD
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(TRIPLE_TARGET x86_64-nacl)
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/prefix" CACHE PATH "Install path prefix, prepended onto install directories." FORCE)
endif ()
get_property(CMAKE_BUILD_TYPE_HELP CACHE CMAKE_BUILD_TYPE PROPERTY HELPSTRING)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "${CMAKE_BUILD_TYPE_HELP}" FORCE)
set(COMPILER_FLAGS "-O3")
find_program(FOUND_CCACHE NAMES "ccache")
option(USE_CCACHE "Enable cached compilation." FOUND_CCACHE)
option(USE_LTO "Enable link-time optimization." ON)
if (USE_LTO)
set(CONFIGURE_ARG_LTO --enable-lto)
endif()
if (USE_CCACHE)
set(CCACHE_CMD "ccache")
endif()
if (USE_CCACHE)
set(CONFIGURE_CC "${CCACHE_CMD} ${CMAKE_C_COMPILER}")
else()
set(CONFIGURE_CC "${CMAKE_C_COMPILER}")
endif()
if (CMAKE_OSX_DEPLOYMENT_TARGET)
set(COMPILER_FLAGS "${COMPILER_FLAGS} -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif()
option(CLONE_SHARED_REPOSITORIES "Clone shared sources dir (and only do that)." OFF)
if (CLONE_SHARED_REPOSITORIES)
set(DEFAULT_SHARED_REPOSITORIES ${CMAKE_CURRENT_BINARY_DIR})
else()
set(DEFAULT_SHARED_REPOSITORIES "")
endif()
set(SHARED_REPOSITORIES_DIR ${DEFAULT_SHARED_REPOSITORIES_DIR} CACHE PATH "Shared repositories dir (doesn't use any if empty).")
set(EXTERNAL_PROJECT_BASE ExternalProjects)
set_directory_properties(PROPERTIES EP_BASE ${CMAKE_CURRENT_BINARY_DIR}/${EXTERNAL_PROJECT_BASE})
if (CLONE_SHARED_REPOSITORIES)
set(EXTERNAL_PROJECT_SOURCES_DIR ${CMAKE_BINARY_DIR})
else()
set(EXTERNAL_PROJECT_SOURCES_DIR ${EXTERNAL_PROJECT_BASE}/Sources)
endif()
macro(AddProject SLUG NAME DIR URL TAG)
set(GIT_REPOSITORY_${SLUG} ${URL} CACHE STRING "${NAME} git repository location.")
mark_as_advanced(GIT_REPOSITORY_${SLANG})
set(REPOSITORY_DIR_${SLUG} ${DIR})
set(REPOSITORY_TAG_${SLUG} ${TAG})
if (CLONE_SHARED_REPOSITORIES)
option(CLONE_${SLUG} "Clone the ${NAME} repository." ON)
if (CLONE_${SLUG})
ExternalProject_Add(${REPOSITORY_DIR_${SLUG}}-shared-repository
SOURCE_DIR ${DIR}
GIT_REPOSITORY ${GIT_REPOSITORY_${SLUG}}
GIT_TAG ${TAG}
CONFIGURE_COMMAND echo
BUILD_COMMAND echo
INSTALL_COMMAND echo
)
endif()
else()
option(BUILD_${SLUG} "Build the ${NAME}." ON)
endif()
if (SHARED_REPOSITORIES_DIR)
set(SOURCE_DIR_${SLUG} ${SHARED_REPOSITORIES_DIR}/${DIR})
set(EP_OPTIONS_${SLUG}
SOURCE_DIR "${SOURCE_DIR_${SLUG}}"
)
else()
set(SOURCE_DIR_${SLUG} ${EXTERNAL_PROJECT_SOURCES_DIR}/${DIR})
set(REPOSITORY_${SLUG} ${GIT_REPOSITORY_${SLUG}})
set(EP_OPTIONS_${SLUG}
SOURCE_DIR "${SOURCE_DIR_${SLUG}}"
GIT_REPOSITORY "${REPOSITORY_${SLUG}}"
GIT_TAG "${REPOSITORY_TAG_${SLUG}}"
)
endif()
endmacro()
AddProject("CLANG"
"NaCl Saigo clang compiler"
"llvm-project"
"https://chromium.googlesource.com/native_client/nacl-llvm-project-v10.git"
"main"
)
AddProject("BINUTILS"
"NaCl binutils"
"binutils"
"https://chromium.googlesource.com/native_client/nacl-binutils.git"
"main"
)
set(REFERENCE_ARCH_NAME x86_64)
set(ALIAS_ARCH_NAMES i686;arm)
macro(RenameBinaryAliases targetName toolNames)
add_custom_target(${targetName}-renames ALL)
add_dependencies(${targetName} ${targetName}-renames)
add_dependencies(${targetName} ${targetName}-renames ${targetName}-binaries)
set(targetPath ${CMAKE_INSTALL_PREFIX}/bin/${targetName}${CMAKE_EXECUTABLE_SUFFIX})
set(referenceName ${targetName}-reference)
set(referencePath ${EXTERNAL_PROJECT_BASE}/tmp/${referenceName}${CMAKE_EXECUTABLE_SUFFIX})
add_custom_target(${referenceName}
ALL
COMMAND cp -P ${targetPath} ${referencePath}
DEPENDS ${targetName}-binaries
)
add_dependencies(${targetName} ${referenceName})
foreach(toolName ${toolNames})
set(aliasName ${REFERENCE_ARCH_NAME}-nacl-${toolName})
set(aliasPath ${CMAKE_INSTALL_PREFIX}/bin/${aliasName}${CMAKE_EXECUTABLE_SUFFIX})
add_custom_target(${aliasName}
ALL
COMMAND cp -P ${referencePath} ${aliasPath}
DEPENDS ${referenceName}
)
add_dependencies(${targetName}-renames ${aliasName})
endforeach()
endmacro()
macro(AddBinaryAliases targetName toolNames)
set(targetAliases ${targetName}-aliases)
add_custom_target(${targetName}-aliases ALL)
add_dependencies(${targetName} ${targetName}-aliases)
add_dependencies(${targetName}-aliases ${targetName}-binaries)
foreach(archName ${ALIAS_ARCH_NAMES})
foreach(toolName ${toolNames})
set(referenceName ${REFERENCE_ARCH_NAME}-nacl-${toolName}${CMAKE_EXECUTABLE_SUFFIX})
set(aliasName ${archName}-nacl-${toolName})
set(aliasPath ${CMAKE_INSTALL_PREFIX}/bin/${aliasName}${CMAKE_EXECUTABLE_SUFFIX})
add_custom_target(${aliasName}
ALL
COMMAND ln -sf ${referenceName} ${aliasPath}
DEPENDS ${targetName}-binaries
)
add_dependencies(${targetName}-aliases ${aliasName})
endforeach()
endforeach()
endmacro()
macro(AddDirectoryAliases targetName toolNames)
add_custom_target(${targetName}-directory-aliases ALL)
add_dependencies(${targetName} ${targetName}-directory-aliases)
add_dependencies(${targetName}-directory-aliases ${targetName}-binaries)
set(referenceName ${REFERENCE_ARCH_NAME}-nacl)
foreach(archName ${ALIAS_ARCH_NAMES})
set(aliasName ${archName}-nacl)
set(aliasPath ${CMAKE_INSTALL_PREFIX}/${aliasName})
add_custom_target(${aliasName}-directory
ALL
COMMAND mkdir -p ${aliasPath}
DEPENDS ${targetName}-binaries
)
add_dependencies(${targetName}-directory-aliases ${aliasName}-directory)
add_custom_target(${aliasName}-bin-directory
ALL
COMMAND ln -sfn ../${referenceName}/bin ${aliasPath}/bin
DEPENDS ${aliasName}-directory
)
add_dependencies(${targetName}-directory-aliases ${aliasName}-bin-directory)
add_custom_target(${aliasName}-lib-directory
ALL
COMMAND mkdir -p ${aliasPath}/lib
DEPENDS ${aliasName}-directory
)
add_dependencies(${targetName}-directory-aliases ${aliasName}-lib-directory)
add_custom_target(${aliasName}-ldscripts-directory
ALL
COMMAND ln -sfn ../../${referenceName}/lib/ldscripts ${aliasPath}/lib/ldscripts
DEPENDS ${aliasName}-lib-directory
)
add_dependencies(${targetName}-directory-aliases ${aliasName}-ldscripts-directory)
endforeach()
foreach(toolName ${toolNames})
set(toolPath ${CMAKE_INSTALL_PREFIX}/${referenceName}/bin/${toolName})
add_custom_target(${targetName}-${toolName}-delete
ALL
COMMAND rm -f ${toolPath}
DEPENDS ${targetName}-binaries
)
add_dependencies(${targetName}-directory-aliases ${targetName}-${toolName}-delete)
add_custom_target(${targetName}-${toolName}-alias
ALL
COMMAND ln -sf ../../bin/${referenceName}-${toolName} ${toolPath}
DEPENDS ${targetName}-${toolName}-delete
)
add_dependencies(${targetName}-directory-aliases ${targetName}-${toolName}-alias)
endforeach()
endmacro()
if (BUILD_CLANG)
add_custom_target(clang ALL)
set(INSTALL_RPATH "@executable_path/../lib")
if (APPLE)
set(INSTALL_RPATH "$ORIGIN/../lib")
endif()
set(USE_IPO ON)
set(USE_LTO OFF)
if (MINGW)
set(USE_IPO OFF)
set(USE_LTO OFF)
elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
set(USE_IPO OFF)
set(USE_LTO Thin)
endif()
if (CMAKE_OSX_DEPLOYMENT_TARGET)
set(CMAKE_EXTRA_ARGS "-DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif()
if (CMAKE_TOOLCHAIN_FILE)
set(CMAKE_ARG_CROSS "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}")
else()
# This is expected when cross-compiling, also sets CMAKE_CROSSCOMPILING.
# See: https://llvm.org/docs/HowToCrossCompileLLVM.html
set(CMAKE_ARG_CROSS "-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
endif()
ExternalProject_Add(clang-binaries
${EP_OPTIONS_CLANG}
SOURCE_SUBDIR llvm
LIST_SEPARATOR |
CMAKE_ARGS "${CMAKE_ARG_CROSS}"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}"
CMAKE_ARGS "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
CMAKE_ARGS "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
CMAKE_ARGS "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
CMAKE_ARGS "-DCMAKE_INSTALL_RPATH=${INSTALL_RPATH}"
CMAKE_ARGS -DLLVM_INSTALL_TOOLCHAIN_ONLY=ON
CMAKE_ARGS -DLLVM_APPEND_VC_REV=ON
CMAKE_ARGS -DLLVM_BUILD_TESTS=OFF
CMAKE_ARGS -DLLVM_ENABLE_ASSERTIONS=ON
CMAKE_ARGS -DLLVM_ENABLE_LIBCXX=OFF
CMAKE_ARGS -DLLVM_ENABLE_ZLIB=OFF
CMAKE_ARGS -DLLVM_ENABLE_ZSTD=OFF
CMAKE_ARGS -DLLVM_ENABLE_Z3_SOLVER=OFF
CMAKE_ARGS "-DLLVM_HOST_TRIPLE=${TRIPLE_HOST}"
CMAKE_ARGS -DLLVM_INSTALL_UTILS=ON
CMAKE_ARGS -DLLVM_ENABLE_LIBXML2=OFF
CMAKE_ARGS "-DLLVM_TARGETS_TO_BUILD=X86|ARM"
CMAKE_ARGS -DLLVM_ENABLE_PROJECTS=clang
CMAKE_ARGS "-DLLVM_DEFAULT_TARGET_TRIPLE=${TRIPLE_TARGET}"
CMAKE_ARGS -DLLVM_BUILD_LLVM_DYLIB=OFF
CMAKE_ARGS -DLLVM_LINK_LLVM_DYLIB=OFF
CMAKE_ARGS -DCLANG_LINK_CLANG_DYLIB=OFF
CMAKE_ARGS -DLLVM_CCACHE_BUILD=OFF
# CMAKE_ARGS -DLLVM_ENABLE_TERMINFO=OFF
CMAKE_ARGS -DLLVM_ENABLE_RUNTIMES=
CMAKE_ARGS -DCLANG_INCLUDE_DOCS=OFF
CMAKE_ARGS -DCLANG_INCLUDE_TESTS=OFF
CMAKE_ARGS -DLLVM_CCACHE_BUILD=${USE_CCACHE}
CMAKE_ARGS -DLLVM_USE_SYMLINKS=ON
CMAKE_ARGS -DLLVM_ENABLE_LTO=${USE_LTO}
CMAKE_ARGS -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=${USE_IPO}
CMAKE_ARGS ${CMAKE_EXTRA_ARGS}
INSTALL_COMMAND ${CMAKE_COMMAND} --install . --strip
)
set(clangToolNames clang;clang++)
RenameBinaryAliases(clang "${clangToolNames}")
AddBinaryAliases(clang "${clangToolNames}")
endif()
if (BUILD_BINUTILS)
add_custom_target(binutils ALL)
if (MINGW OR CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(STATIC_LIBGCC "-static-libgcc")
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(CONFIGURE_ARG_BUILD "--build=${TRIPLE_BUILD}")
set(INSTALL_ARG_STRIP "STRIPPROG=${TRIPLE_HOST}-strip")
endif()
ExternalProject_Add(binutils-binaries
${EP_OPTIONS_BINUTILS}
CONFIGURE_COMMAND "${SOURCE_DIR_BINUTILS}/configure"
CONFIGURE_COMMAND "MAKEINFO=true"
CONFIGURE_COMMAND "CC=${CONFIGURE_CC}"
CONFIGURE_COMMAND "CFLAGS=${COMPILER_FLAGS} -Wno-absolute-value -Wno-unused-function -Wno-unused-const-variable -Wno-unneeded-internal-declaration -Wno-unused-private-field -Wno-format-security"
CONFIGURE_COMMAND "LDFLAGS=${LINKER_FLAGS} -static-libstdc++ ${STATIC_LIBGCC}"
CONFIGURE_COMMAND "${CONFIGURE_ARG_LTO}"
CONFIGURE_COMMAND "${CONFIGURE_ARG_BUILD}"
CONFIGURE_COMMAND --prefix=
CONFIGURE_COMMAND "--host=${TRIPLE_HOST}"
CONFIGURE_COMMAND "--target=${TRIPLE_TARGET}"
CONFIGURE_COMMAND --enable-targets=x86_64-nacl,i686-nacl,arm-nacl
# CONFIGURE_COMMAND --enable-gold --enable-plugins --disable-shared
CONFIGURE_COMMAND --disable-gold
CONFIGURE_COMMAND --disable-werror
CONFIGURE_COMMAND --without-zlib
CONFIGURE_COMMAND --disable-silent-rules
CONFIGURE_COMMAND --enable-deterministic-archives
BUILD_COMMAND $(MAKE)
BUILD_COMMAND MAKEINFO=true
INSTALL_COMMAND $(MAKE)
INSTALL_COMMAND MAKEINFO=true
INSTALL_COMMAND ${INSTALL_ARG_STRIP}
INSTALL_COMMAND DESTDIR=${CMAKE_INSTALL_PREFIX}
INSTALL_COMMAND install-strip
)
set(binutilsToolNames addr2line;ar;as;c++filt;elfedit;gprof;nm;objcopy;objdump;ranlib;readelf;size;strings;strip;ld;ld.bfd) # ld.gold
set(binutilsDirectoryToolNames ar;as;ld;ld.bfd;nm;objcopy;objdump;ranlib;strip) # ld.gold
AddBinaryAliases(binutils "${binutilsToolNames}")
AddDirectoryAliases(binutils "${binutilsDirectoryToolNames}")
endif()