forked from WebAssembly/binaryen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
323 lines (283 loc) · 11.8 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
PROJECT(binaryen C CXX)
CMAKE_MINIMUM_REQUIRED(VERSION 3.1.3)
INCLUDE(GNUInstallDirs)
IF(NOT CMAKE_BUILD_TYPE)
MESSAGE(STATUS "No build type selected, default to Release")
SET(CMAKE_BUILD_TYPE "Release")
ENDIF()
FIND_PACKAGE(Git QUIET REQUIRED)
EXECUTE_PROCESS(COMMAND
"${GIT_EXECUTABLE}" --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git describe --tags
RESULT_VARIABLE
GIT_HASH_RESULT
OUTPUT_VARIABLE
GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
IF(${GIT_HASH_RESULT})
MESSAGE(WARNING "Error running git describe to determine version")
SET(BINARYEN_VERSION_INFO "(unable to determine version)")
ELSE()
SET(BINARYEN_VERSION_INFO "${GIT_HASH}")
ENDIF()
CONFIGURE_FILE(config.h.in config.h)
OPTION(BUILD_STATIC_LIB "Build as a static library" OFF)
# Support functionality.
FUNCTION(ADD_COMPILE_FLAG value)
MESSAGE(STATUS "Building with ${value}")
FOREACH(variable CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
SET(${variable} "${${variable}} ${value}" PARENT_SCOPE)
ENDFOREACH(variable)
ENDFUNCTION()
FUNCTION(ADD_CXX_FLAG value)
MESSAGE(STATUS "Building with ${value}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}" PARENT_SCOPE)
ENDFUNCTION()
FUNCTION(ADD_DEBUG_COMPILE_FLAG value)
IF("${CMAKE_BUILD_TYPE}" MATCHES "Debug")
MESSAGE(STATUS "Building with ${value}")
ENDIF()
FOREACH(variable CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG)
SET(${variable} "${${variable}} ${value}" PARENT_SCOPE)
ENDFOREACH(variable)
ENDFUNCTION()
FUNCTION(ADD_NONDEBUG_COMPILE_FLAG value)
IF(NOT "${CMAKE_BUILD_TYPE}" MATCHES "Debug")
MESSAGE(STATUS "Building with ${value}")
ENDIF()
FOREACH(variable CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_C_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_MINSIZEREL)
SET(${variable} "${${variable}} ${value}" PARENT_SCOPE)
ENDFOREACH(variable)
ENDFUNCTION()
FUNCTION(ADD_LINK_FLAG value)
MESSAGE(STATUS "Linking with ${value}")
FOREACH(variable CMAKE_EXE_LINKER_FLAGS)
SET(${variable} "${${variable}} ${value}" PARENT_SCOPE)
ENDFOREACH(variable)
ENDFUNCTION()
# Compiler setup.
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/src)
# Add output directory to include path so config.h can be found
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
# Force output to bin/ and lib/. This is to suppress CMake multigenerator output paths and avoid bin/Debug, bin/Release/ and so on, which is CMake default.
FOREACH(SUFFIX "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL" "")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/bin")
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib")
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY${SUFFIX} "${PROJECT_BINARY_DIR}/lib")
ENDFOREACH()
IF(MSVC)
IF(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.0") # VS2013 and older explicitly need /arch:sse2 set, VS2015 no longer has that option, but always enabled.
ADD_COMPILE_FLAG("/arch:sse2")
ENDIF()
ADD_COMPILE_FLAG("/wd4146") # Ignore warning "warning C4146: unary minus operator applied to unsigned type, result still unsigned", this pattern is used somewhat commonly in the code.
# 4267 and 4244 are conversion/truncation warnings. We might want to fix these but they are currently pervasive.
ADD_COMPILE_FLAG("/wd4267")
ADD_COMPILE_FLAG("/wd4244")
# 4722 warns that destructors never return, even with WASM_NORETURN.
ADD_COMPILE_FLAG("/wd4722")
ADD_COMPILE_FLAG("/WX-")
ADD_DEBUG_COMPILE_FLAG("/Od")
ADD_NONDEBUG_COMPILE_FLAG("/O2")
ADD_COMPILE_FLAG("/D_CRT_SECURE_NO_WARNINGS")
ADD_COMPILE_FLAG("/D_SCL_SECURE_NO_WARNINGS")
# Visual Studio 2018 15.8 implemented conformant support for std::aligned_storage, but the conformant support is only enabled when the following flag is passed, to avoid
# breaking backwards compatibility with code that relied on the non-conformant behavior (the old nonconformant behavior is not used with Binaryen)
ADD_COMPILE_FLAG("/D_ENABLE_EXTENDED_ALIGNED_STORAGE")
# Don't warn about using "strdup" as a reserved name.
ADD_COMPILE_FLAG("/D_CRT_NONSTDC_NO_DEPRECATE")
ADD_NONDEBUG_COMPILE_FLAG("/UNDEBUG") # Keep asserts.
# Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
if( NOT CMAKE_BUILD_TYPE MATCHES "Debug" )
foreach (flags_var_to_scrub
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_MINSIZEREL)
string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
# Compile with `/MT` to link against `libcmt.lib`, removing a dependency
# on `msvcrt.dll`. May result in slightly larger binaries but they should
# be more portable across systems.
string(REPLACE "/MD" "/MT" ${flags_var_to_scrub} "${${flags_var_to_scrub}}")
endforeach()
endif()
ADD_LINK_FLAG("/STACK:8388608")
IF(RUN_STATIC_ANALYZER)
ADD_DEFINITIONS(/analyze)
ENDIF()
ELSE()
OPTION(ENABLE_WERROR "Enable -Werror" ON)
SET(THREADS_PREFER_PTHREAD_FLAG ON)
SET(CMAKE_THREAD_PREFER_PTHREAD ON)
FIND_PACKAGE(Threads REQUIRED)
ADD_CXX_FLAG("-std=c++14")
if (NOT EMSCRIPTEN)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
# wasm doesn't allow for x87 floating point math
ADD_COMPILE_FLAG("-msse2")
ADD_COMPILE_FLAG("-mfpmath=sse")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^armv[2-6]" AND NOT CMAKE_CXX_FLAGS MATCHES "-mfpu=")
ADD_COMPILE_FLAG("-mfpu=vfpv3")
endif ()
endif ()
ADD_COMPILE_FLAG("-Wall")
if(ENABLE_WERROR)
ADD_COMPILE_FLAG("-Werror")
endif()
ADD_COMPILE_FLAG("-Wextra")
ADD_COMPILE_FLAG("-Wno-unused-parameter")
ADD_COMPILE_FLAG("-fno-omit-frame-pointer")
# TODO(https://github.com/WebAssembly/binaryen/pull/2314): Remove these two
# flags once we resolve the issue.
ADD_COMPILE_FLAG("-Wno-implicit-int-float-conversion")
ADD_COMPILE_FLAG("-Wno-unknown-warning-option")
ADD_COMPILE_FLAG("-Wswitch") # we explicitly expect this in the code
IF(WIN32)
ADD_COMPILE_FLAG("-D_GNU_SOURCE")
ADD_LINK_FLAG("-Wl,--stack,8388608")
ELSE()
ADD_COMPILE_FLAG("-fPIC")
ENDIF()
ADD_DEBUG_COMPILE_FLAG("-O0")
ADD_DEBUG_COMPILE_FLAG("-g3")
ADD_NONDEBUG_COMPILE_FLAG("-O2")
ADD_NONDEBUG_COMPILE_FLAG("-UNDEBUG") # Keep asserts.
ENDIF()
# clang doesn't print colored diagnostics when invoked from Ninja
IF (UNIX AND
CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
CMAKE_GENERATOR STREQUAL "Ninja")
ADD_COMPILE_FLAG("-fcolor-diagnostics")
ENDIF()
# Static libraries
# Current (partial) dependency structure is as follows:
# passes -> wasm -> asmjs -> support
# TODO: It's odd that wasm should depend on asmjs, maybe we should fix that.
ADD_SUBDIRECTORY(src/ir)
ADD_SUBDIRECTORY(src/asmjs)
ADD_SUBDIRECTORY(src/cfg)
ADD_SUBDIRECTORY(src/emscripten-optimizer)
ADD_SUBDIRECTORY(src/passes)
ADD_SUBDIRECTORY(src/support)
ADD_SUBDIRECTORY(src/wasm)
# Sources.
SET(binaryen_SOURCES
src/binaryen-c.cpp
)
IF(BUILD_STATIC_LIB)
ADD_LIBRARY(binaryen STATIC ${binaryen_SOURCES})
ADD_DEFINITIONS(-DBUILD_STATIC_LIBRARY)
ELSE()
ADD_LIBRARY(binaryen SHARED ${binaryen_SOURCES})
ENDIF()
TARGET_LINK_LIBRARIES(binaryen passes wasm asmjs emscripten-optimizer ir cfg support)
INSTALL(TARGETS binaryen DESTINATION ${CMAKE_INSTALL_LIBDIR})
INSTALL(FILES src/binaryen-c.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# if binaryen.js was built (using "./build-js.sh", currently
# optional), install it
IF(EXISTS "${PROJECT_SOURCE_DIR}/out/binaryen.js")
INSTALL(FILES out/binaryen.js DESTINATION ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME})
ENDIF()
SET(wasm-shell_SOURCES
src/tools/wasm-shell.cpp
)
ADD_EXECUTABLE(wasm-shell
${wasm-shell_SOURCES})
TARGET_LINK_LIBRARIES(wasm-shell wasm asmjs emscripten-optimizer passes ir cfg support wasm)
SET_PROPERTY(TARGET wasm-shell PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-shell PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-shell DESTINATION ${CMAKE_INSTALL_BINDIR})
SET(wasm-opt_SOURCES
src/tools/wasm-opt.cpp
)
ADD_EXECUTABLE(wasm-opt
${wasm-opt_SOURCES})
TARGET_LINK_LIBRARIES(wasm-opt wasm asmjs emscripten-optimizer passes ir cfg support wasm)
SET_PROPERTY(TARGET wasm-opt PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-opt PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-opt DESTINATION ${CMAKE_INSTALL_BINDIR})
SET(wasm-metadce_SOURCES
src/tools/wasm-metadce.cpp
)
ADD_EXECUTABLE(wasm-metadce
${wasm-metadce_SOURCES})
TARGET_LINK_LIBRARIES(wasm-metadce wasm asmjs emscripten-optimizer passes ir cfg support wasm)
SET_PROPERTY(TARGET wasm-metadce PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-metadce PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-metadce DESTINATION bin)
SET(asm2wasm_SOURCES
src/tools/asm2wasm.cpp
)
ADD_EXECUTABLE(asm2wasm
${asm2wasm_SOURCES})
TARGET_LINK_LIBRARIES(asm2wasm emscripten-optimizer passes wasm asmjs ir cfg support)
SET_PROPERTY(TARGET asm2wasm PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET asm2wasm PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS asm2wasm DESTINATION ${CMAKE_INSTALL_BINDIR})
SET(wasm2js_SOURCES
src/tools/wasm2js.cpp
)
ADD_EXECUTABLE(wasm2js
${wasm2js_SOURCES})
TARGET_LINK_LIBRARIES(wasm2js passes wasm asmjs emscripten-optimizer ir cfg support)
SET_PROPERTY(TARGET wasm2js PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm2js PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm2js DESTINATION ${CMAKE_INSTALL_BINDIR})
SET(wasm-emscripten-finalize_SOURCES
src/tools/wasm-emscripten-finalize.cpp
)
ADD_EXECUTABLE(wasm-emscripten-finalize
${wasm-emscripten-finalize_SOURCES})
TARGET_LINK_LIBRARIES(wasm-emscripten-finalize passes wasm asmjs ir cfg support)
SET_PROPERTY(TARGET wasm-emscripten-finalize PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-emscripten-finalize PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-emscripten-finalize DESTINATION ${CMAKE_INSTALL_BINDIR})
SET(wasm_as_SOURCES
src/tools/wasm-as.cpp
)
ADD_EXECUTABLE(wasm-as
${wasm_as_SOURCES})
TARGET_LINK_LIBRARIES(wasm-as wasm asmjs passes ir cfg support wasm)
SET_PROPERTY(TARGET wasm-as PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-as PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-as DESTINATION ${CMAKE_INSTALL_BINDIR})
SET(wasm_dis_SOURCES
src/tools/wasm-dis.cpp
)
ADD_EXECUTABLE(wasm-dis
${wasm_dis_SOURCES})
TARGET_LINK_LIBRARIES(wasm-dis passes wasm asmjs ir cfg support)
SET_PROPERTY(TARGET wasm-dis PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-dis PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-dis DESTINATION ${CMAKE_INSTALL_BINDIR})
SET(wasm-ctor-eval_SOURCES
src/tools/wasm-ctor-eval.cpp
)
ADD_EXECUTABLE(wasm-ctor-eval
${wasm-ctor-eval_SOURCES})
TARGET_LINK_LIBRARIES(wasm-ctor-eval emscripten-optimizer passes wasm asmjs ir cfg support)
SET_PROPERTY(TARGET wasm-ctor-eval PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-ctor-eval PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-ctor-eval DESTINATION bin)
SET(wasm-reduce_SOURCES
src/tools/wasm-reduce.cpp
)
ADD_EXECUTABLE(wasm-reduce
${wasm-reduce_SOURCES})
TARGET_LINK_LIBRARIES(wasm-reduce wasm asmjs passes wasm ir cfg support)
SET_PROPERTY(TARGET wasm-reduce PROPERTY CXX_STANDARD 14)
SET_PROPERTY(TARGET wasm-reduce PROPERTY CXX_STANDARD_REQUIRED ON)
INSTALL(TARGETS wasm-reduce DESTINATION ${CMAKE_INSTALL_BINDIR})
# Testing
#
# Currently just some very simple smoke tests.
ENABLE_TESTING()
ADD_TEST(NAME opt-unit
COMMAND bin/wasm-opt test/unit.wast --flatten --ssa --metrics -O4 -Os --metrics)
ADD_TEST(NAME metrics-emcc
COMMAND bin/wasm-opt test/emcc_hello_world.fromasm --metrics)
ADD_TEST(NAME exec-unit
COMMAND bin/wasm-opt test/unit.wast --fuzz-exec)
ADD_TEST(NAME exec-hello
COMMAND bin/wasm-opt test/hello_world.wast --fuzz-exec)