-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
260 lines (206 loc) · 8.63 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
# Author: Thomas Debesse
# License: CC0 1.0
cmake_minimum_required(VERSION 3.5)
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
project(SaigoNativeClientToolchain)
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")
option(USE_LTO "Enable link-time optimization." OFF)
option(USE_CCACHE "Enable cached compilation." ON)
option(USE_SYMLINKS "Use symlinks instead of copying binaries" ${CMAKE_HOST_UNIX})
if (USE_LTO)
set(CONFIGURE_ARG_LTO --enable-lto)
endif()
if (USE_CCACHE)
set(CCACHE_CMD "ccache")
endif()
option(BUILD_CLANG "Build the NaCl Saigo clang compiler." ON)
option(BUILD_BINUTILS "Build the NaCl binutils." ON)
set(GIT_REPOSITORY_CLANG "https://chromium.googlesource.com/native_client/nacl-llvm-project-v10.git" CACHE STRING "Git repository location.")
set(GIT_REPOSITORY_BINUTILS "https://chromium.googlesource.com/native_client/nacl-binutils.git" CACHE STRING "Git repository location.")
mark_as_advanced(GIT_REPOSITORY_CLANG)
mark_as_advanced(GIT_REPOSITORY_BINUTILS)
set(REFERENCE_ARCH_NAME x86_64)
set(ALIAS_ARCH_NAMES i686;arm)
set(EXTERNAL_PROJECT_BASE ExternalProjects)
set(EXTERNAL_PROJECT_SOURCES_DIR ${EXTERNAL_PROJECT_BASE}/Sources)
set_directory_properties(PROPERTIES EP_BASE ${CMAKE_CURRENT_BINARY_DIR}/${EXTERNAL_PROJECT_BASE})
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})
set(referenceName ${targetName}-reference)
set(referencePath ${EXTERNAL_PROJECT_BASE}/tmp/${referenceName})
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})
add_custom_target(${aliasName}
ALL
COMMAND cp -P ${referencePath} ${aliasPath}
DEPENDS ${referenceName}
)
add_dependencies(${targetName}-renames ${aliasName})
set(deleteName ${toolName}-delete)
set(deleteFullPath ${CMAKE_INSTALL_PREFIX}/bin/${toolName})
add_custom_target(${deleteName}
ALL
COMMAND rm -f ${deleteFullPath}
DEPENDS ${toolFullName}
)
add_dependencies(${targetName}-renames ${deleteName})
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})
set(aliasName ${archName}-nacl-${toolName})
set(aliasPath ${CMAKE_INSTALL_PREFIX}/bin/${aliasName})
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 -sfT ../${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 -sfT ../../${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(sourceDir ${EXTERNAL_PROJECT_SOURCES_DIR}/llvm-project)
ExternalProject_Add(clang-binaries
SOURCE_DIR ${sourceDir}
GIT_REPOSITORY ${GIT_REPOSITORY_CLANG}
GIT_TAG main
SOURCE_SUBDIR llvm
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=%s/../lib
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_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=x86_64-nacl
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_ENABLE_LTO=${USE_LTO}
CMAKE_ARGS -DLLVM_CCACHE_BUILD=${USE_CCACHE}
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)
set(sourceDir ${EXTERNAL_PROJECT_SOURCES_DIR}/binutils)
set(sourceFullDir ${CMAKE_CURRENT_BINARY_DIR}/${sourceDir})
ExternalProject_Add(binutils-binaries
SOURCE_DIR ${sourceDir}
GIT_REPOSITORY ${GIT_REPOSITORY_BINUTILS}
GIT_TAG main
CONFIGURE_COMMAND ${sourceFullDir}/configure
CONFIGURE_COMMAND LD=lld
CONFIGURE_COMMAND "CC=${CCACHE_CMD} ${CMAKE_C_COMPILER}"
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=-fuse-ld=lld -static-libstdc++ -static-libgcc"
CONFIGURE_COMMAND "LDFLAGS=-static-libstdc++ -static-libgcc"
CONFIGURE_COMMAND ${CONFIGURE_ARG_LTO}
CONFIGURE_COMMAND --prefix=
CONFIGURE_COMMAND --target=x86_64-nacl
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) MAKEINFO=true
INSTALL_COMMAND $(MAKE) MAKEINFO=true install-strip
INSTALL_COMMAND DESTDIR=${CMAKE_INSTALL_PREFIX}
)
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()