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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
# Version set according the the cmake versions available in Ubuntu/Bionic:
# https://packages.ubuntu.com/bionic/cmake
cmake_minimum_required(VERSION 3.10.2)
# Needed for C++17 (std::variant)
# TODO(https://github.com/WebAssembly/binaryen/issues/4299): We need
# to reduce this for compatability with emsdk.
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14" CACHE STRING "Minimum OS X deployment version")
project(binaryen LANGUAGES C CXX VERSION 108)
include(GNUInstallDirs)
# The C++ standard whose features are required to build Binaryen.
# Keep in sync with scripts/test/shared.py cxx_standard
# The if condition allows embedding in a project with a higher default C++ standard set
set(REQUIRED_CXX_STANDARD 17)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD ${REQUIRED_CXX_STANDARD})
elseif(CMAKE_CXX_STANDARD LESS ${REQUIRED_CXX_STANDARD})
message(SEND_ERROR "Building with C++ standards older than C++${REQUIRED_CXX_STANDARD} is not supported, change CMAKE_CXX_STANDARD to ${REQUIRED_CXX_STANDARD} or later")
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type selected, default to Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
# We default to assertions enabled, even in release builds so that we get
# more useful error reports from users.
option(BYN_ENABLE_ASSERTIONS "Enable assertions" ON)
# Turn this off to avoid the dependency on gtest.
option(BUILD_TESTS "Build GTest-based tests" ON)
# Turn this off to build only the library.
option(BUILD_TOOLS "Build tools" ON)
# Turn this on to build binaryen.js as ES5, with additional compatibility configuration for js_of_ocaml.
option(JS_OF_OCAML "Build binaryen.js for js_of_ocaml" OFF)
# For git users, attempt to generate a more useful version string
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git)
find_package(Git QUIET REQUIRED)
execute_process(COMMAND
"${GIT_EXECUTABLE}" --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git describe --tags --match version_*
RESULT_VARIABLE
GIT_VERSION_RESULT
OUTPUT_VARIABLE
GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(${GIT_VERSION_RESULT})
message(WARNING "Error running git describe to determine version")
else()
set(PROJECT_VERSION "${PROJECT_VERSION} (${GIT_VERSION})")
endif()
endif()
configure_file(config.h.in config.h)
# 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 CMAKE_SHARED_LINKER_FLAGS)
set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
endforeach(variable)
endfunction()
function(binaryen_setup_rpath name)
if(CMAKE_INSTALL_RPATH)
return()
endif()
if(APPLE)
set(_install_name_dir INSTALL_NAME_DIR "@rpath")
set(_install_rpath "@loader_path/../lib")
elseif(UNIX)
set(_install_rpath "\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
set_property(TARGET ${name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-z,origin ")
endif()
else()
return()
endif()
set_target_properties(${name} PROPERTIES
BUILD_WITH_INSTALL_RPATH On
INSTALL_RPATH "${_install_rpath}"
${_install_name_dir})
endfunction()
function(binaryen_add_executable name sources)
add_executable(${name} ${sources})
target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(${name} binaryen)
binaryen_setup_rpath(${name})
install(TARGETS ${name} DESTINATION ${CMAKE_INSTALL_BINDIR})
endfunction()
# Options
option(BUILD_STATIC_LIB "Build as a static library" OFF)
if(MSVC)
# We don't have dllexport declarations set up for windows yet.
set(BUILD_STATIC_LIB ON)
endif()
# For now, don't include full DWARF support in JS builds, for size.
if(NOT EMSCRIPTEN)
option(BUILD_LLVM_DWARF "Enable full DWARF support" ON)
if(BUILD_LLVM_DWARF)
if(MSVC)
ADD_COMPILE_FLAG("/DBUILD_LLVM_DWARF")
else()
ADD_COMPILE_FLAG("-DBUILD_LLVM_DWARF")
endif()
endif()
endif()
# Compiler setup.
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
if(BUILD_LLVM_DWARF)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm-project/include)
endif()
# 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()
option(BYN_ENABLE_LTO "Build with LTO" Off)
if(BYN_ENABLE_LTO)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(FATAL_ERROR "ThinLTO is only supported by clang")
endif()
add_link_flag("-fuse-ld=lld")
set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=2)
set(CMAKE_JOB_POOL_LINK link_job_pool)
add_compile_flag("-flto=thin")
endif()
if(MSVC)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# multi-core build.
add_compile_flag("/MP")
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()
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 [[noreturn]].
add_compile_flag("/wd4722")
# "destructor was implicitly defined as deleted" caused by LLVM headers.
add_compile_flag("/wd4624")
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")
# workaround for https://github.com/WebAssembly/binaryen/issues/3661
add_compile_flag("/D_SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING")
# 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")
if(BYN_ENABLE_ASSERTIONS)
# On non-Debug builds cmake automatically defines NDEBUG, so we
# explicitly undefine it:
add_nondebug_compile_flag("/UNDEBUG") # Keep asserts.
endif()
# 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)
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")
add_compile_flag("-fno-rtti")
# 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
add_compile_flag("-Wimplicit-fallthrough")
add_compile_flag("-Wnon-virtual-dtor")
if(WIN32)
add_compile_flag("-D_GNU_SOURCE")
add_compile_flag("-D__STDC_FORMAT_MACROS")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_link_flag("-Wl,/stack:8388608")
else()
add_link_flag("-Wl,--stack,8388608")
endif()
elseif(NOT EMSCRIPTEN)
add_compile_flag("-fPIC")
endif()
add_debug_compile_flag("-O0")
add_debug_compile_flag("-g3")
add_nondebug_compile_flag("-O2")
if(BYN_ENABLE_ASSERTIONS)
# On non-Debug builds cmake automatically defines NDEBUG, so we
# explicitly undefine it:
add_nondebug_compile_flag("-UNDEBUG")
endif()
endif()
if(EMSCRIPTEN)
# link with -O3 for metadce and other powerful optimizations. note that we
# must use add_link_options so that this appears after CMake's default -O2
add_link_options("-O3")
add_link_flag("-s SINGLE_FILE")
add_link_flag("-s ALLOW_MEMORY_GROWTH=1")
add_compile_flag("-s DISABLE_EXCEPTION_CATCHING=0")
add_link_flag("-s DISABLE_EXCEPTION_CATCHING=0")
# make the tools immediately usable on Node.js
add_link_flag("-s NODERAWFS")
# in opt builds, LTO helps so much (>20%) it's worth slow compile times
add_nondebug_compile_flag("-flto")
endif()
# clang doesn't print colored diagnostics when invoked from Ninja
if(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_flag("-fdiagnostics-color=always")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_flag("-fcolor-diagnostics")
endif()
endif()
# Static libraries
# Current (partial) dependency structure is as follows:
# tools -> 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)
if(BUILD_TOOLS)
# Build binaryen tools
add_subdirectory(src/tools)
endif()
add_subdirectory(third_party)
# Configure lit tests
add_subdirectory(test/lit)
if(BUILD_TESTS)
# Configure GTest unit tests
add_subdirectory(test/gtest)
endif()
# Object files
set(binaryen_objs
$<TARGET_OBJECTS:passes>
$<TARGET_OBJECTS:wasm>
$<TARGET_OBJECTS:asmjs>
$<TARGET_OBJECTS:emscripten-optimizer>
$<TARGET_OBJECTS:ir>
$<TARGET_OBJECTS:cfg>
$<TARGET_OBJECTS:support>)
if(BUILD_LLVM_DWARF)
SET(binaryen_objs ${binaryen_objs} $<TARGET_OBJECTS:llvm_dwarf>)
endif()
# Sources.
file(GLOB binaryen_HEADERS src/*.h)
set(binaryen_SOURCES
src/binaryen-c.cpp
${binaryen_HEADERS}
)
if(BUILD_STATIC_LIB)
message(STATUS "Building libbinaryen as statically linked library.")
add_library(binaryen STATIC ${binaryen_SOURCES} ${binaryen_objs})
add_definitions(-DBUILD_STATIC_LIBRARY)
else()
message(STATUS "Building libbinaryen as shared library.")
add_library(binaryen SHARED ${binaryen_SOURCES} ${binaryen_objs})
endif()
if(NOT (BUILD_STATIC_LIB AND BYN_INSTALL_TOOLS_ONLY))
install(TARGETS binaryen
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
if(NOT BYN_INSTALL_TOOLS_ONLY)
install(FILES src/binaryen-c.h src/wasm-delegations.def DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()
# binaryen.js
#
# Note that we can't emit binaryen.js directly, as there is libbinaryen already
# declared earlier, so we create binaryen_wasm/js.js, which must then be copied.
# Note that SHELL: is needed as otherwise cmake will coalesce -s link flags
# in an incorrect way for emscripten.
if(EMSCRIPTEN)
set(binaryen_emscripten_SOURCES
src/binaryen-c.cpp
${binaryen_HEADERS}
)
# binaryen.js WebAssembly variant
add_executable(binaryen_wasm
${binaryen_emscripten_SOURCES})
target_link_libraries(binaryen_wasm wasm asmjs emscripten-optimizer passes ir cfg support wasm)
target_link_libraries(binaryen_wasm "-s NO_FILESYSTEM=0")
target_link_libraries(binaryen_wasm "-s NODERAWFS=0")
target_link_libraries(binaryen_wasm "-s EXPORT_NAME=Binaryen")
target_link_libraries(binaryen_wasm "-s EXPORT_ES6=1")
target_link_libraries(binaryen_wasm "--post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-post.js")
target_link_libraries(binaryen_wasm "--extern-pre-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-extern-pre.js")
target_link_libraries(binaryen_wasm optimized "--closure 1")
target_link_libraries(binaryen_wasm optimized "--closure-args \"--language_in=ECMASCRIPT6 --language_out=ECMASCRIPT6\"")
target_link_libraries(binaryen_wasm optimized "-flto")
target_link_libraries(binaryen_wasm debug "--profiling")
install(TARGETS binaryen_wasm DESTINATION ${CMAKE_INSTALL_BINDIR})
# binaryen.js JavaScript variant
add_executable(binaryen_js
${binaryen_emscripten_SOURCES})
target_link_libraries(binaryen_js wasm asmjs emscripten-optimizer passes ir cfg support wasm)
target_link_libraries(binaryen_js "-s WASM=0")
target_link_libraries(binaryen_js "-s WASM_ASYNC_COMPILATION=0")
if(${CMAKE_CXX_COMPILER_VERSION} STREQUAL "6.0.1")
# only valid with fastcomp and WASM=0
target_link_libraries(binaryen_js "-s ELIMINATE_DUPLICATE_FUNCTIONS=1")
endif()
# Disabling filesystem and setting web environment for js_of_ocaml
# so it doesn't try to detect the "node" environment
if(JS_OF_OCAML)
target_link_libraries(binaryen_js "-s NO_FILESYSTEM=1")
target_link_libraries(binaryen_js "-s ENVIRONMENT=web,worker")
else()
target_link_libraries(binaryen_js "-s NO_FILESYSTEM=0")
endif()
target_link_libraries(binaryen_js "-s NODERAWFS=0")
target_link_libraries(binaryen_js "-s EXPORT_NAME=Binaryen")
# Currently, js_of_ocaml can only process ES5 code
if(JS_OF_OCAML)
target_link_libraries(binaryen_js "-s EXPORT_ES6=0")
else()
target_link_libraries(binaryen_js "-s EXPORT_ES6=1")
endif()
target_link_libraries(binaryen_js "--post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-post.js")
# js_of_ocaml needs a specified variable with special comment to provide the library to consumers
if(JS_OF_OCAML)
target_link_libraries(binaryen_js "--extern-pre-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.jsoo-extern-pre.js")
else()
target_link_libraries(binaryen_js "--extern-pre-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-extern-pre.js")
endif()
target_link_libraries(binaryen_js optimized "--closure 1")
# Currently, js_of_ocaml can only process ES5 code
if(JS_OF_OCAML)
target_link_libraries(binaryen_js optimized "--closure-args \"--language_in=ECMASCRIPT6 --language_out=ECMASCRIPT5\"")
else()
target_link_libraries(binaryen_js optimized "--closure-args \"--language_in=ECMASCRIPT6 --language_out=ECMASCRIPT6\"")
endif()
target_link_libraries(binaryen_js optimized "-flto")
target_link_libraries(binaryen_js debug "--profiling")
target_link_libraries(binaryen_js debug "-s ASSERTIONS")
install(TARGETS binaryen_js DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
configure_file(scripts/binaryen-lit.in ${CMAKE_BINARY_DIR}/bin/binaryen-lit @ONLY)
|