| LEFT | RIGHT | 
|---|
| 1 # Set the minimum we require to the minimum that Emscripten requires. | 1 # Set the minimum we require to the minimum that Emscripten requires. | 
| 2 # See the Emscripten toolchain file for details. | 2 # See the Emscripten toolchain file for details. | 
| 3 cmake_minimum_required(VERSION 3.4.3) | 3 cmake_minimum_required(VERSION 3.4.3) | 
|  | 4 project (adblockpluscore) | 
| 4 | 5 | 
| 5 project (adblockpluscore) | 6 # | 
|  | 7 # Configurations | 
|  | 8 #   - Release. The default with no special compile or link flags. | 
|  | 9 #   - ReleaseTrace. Enables tracing. | 
|  | 10 #   - Debug. Turns on debugging. | 
|  | 11 #   - DebugTrace. Turns on debugging and enables tracing. | 
|  | 12 # | 
|  | 13 set(AVAILABLE_CONFIGURATIONS "Release;ReleaseTrace;Debug;DebugTrace") | 
|  | 14 if(CMAKE_CONFIGURATION_TYPES) | 
|  | 15   # We are using a multiconfiguration generator. | 
|  | 16   set(CMAKE_CONFIGURATION_TYPES ${AVAILABLE_CONFIGURATIONS} | 
|  | 17       CACHE STRING "" FORCE) | 
|  | 18 else() | 
|  | 19   # We are using a single-configuration generator. | 
|  | 20   if(NOT CMAKE_BUILD_TYPE) | 
|  | 21     message("Build type not specified explicitly. Defaulting to 'Release'.") | 
|  | 22     set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE) | 
|  | 23   endif() | 
|  | 24   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY | 
|  | 25                HELPSTRING "Available build types") | 
|  | 26   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY | 
|  | 27                STRINGS ${AVAILABLE_CONFIGURATIONS}) | 
|  | 28 endif() | 
|  | 29 | 
|  | 30 # | 
|  | 31 # Remove variables for the default configurations | 
|  | 32 #   This is a workaround for a poorly documented feature in CMake. Setting | 
|  | 33 #   these toolchain variables is *not* destructive assignment but rather | 
|  | 34 #   an append operation. Removing the variable has an effect equivalent to | 
|  | 35 #   setting its value to empty. | 
|  | 36 # | 
|  | 37 foreach(FLAG_TYPE IN ITEMS C CXX EXE_LINKER MODULE_LINKER SHARED_LINKER STATIC_L
     INKER) | 
|  | 38   foreach(BUILTIN_CONFIG IN ITEMS RELEASE DEBUG MINSIZEREL RELWITHDEBINFO) | 
|  | 39     unset(CMAKE_${FLAG_TYPE}_FLAGS_${BUILTIN_CONFIG}) | 
|  | 40     unset(CMAKE_${FLAG_TYPE}_FLAGS_${BUILTIN_CONFIG} CACHE) | 
|  | 41   endforeach() | 
|  | 42 endforeach() | 
|  | 43 # | 
|  | 44 # Set up the flags for our own configurations. | 
|  | 45 # | 
|  | 46 set(CONFIG_DOC_STRING "Overridden by adblockpluscore") | 
|  | 47 # Release | 
|  | 48 set(CMAKE_C_FLAGS_RELEASE "" CACHE STRING ${CONFIG_DOC_STRING} FORCE) | 
|  | 49 set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING ${CONFIG_DOC_STRING} FORCE) | 
|  | 50 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "" CACHE STRING ${CONFIG_DOC_STRING} FORCE) | 
|  | 51 # ReleaseTrace | 
|  | 52 set(CMAKE_C_FLAGS_RELEASETRACE "--tracing" CACHE STRING ${CONFIG_DOC_STRING} FOR
     CE) | 
|  | 53 set(CMAKE_CXX_FLAGS_RELEASETRACE "--tracing" CACHE STRING ${CONFIG_DOC_STRING} F
     ORCE) | 
|  | 54 set(CMAKE_EXE_LINKER_FLAGS_RELEASETRACE "--tracing" CACHE STRING ${CONFIG_DOC_ST
     RING} FORCE) | 
|  | 55 # Debug | 
|  | 56 set(CMAKE_C_FLAGS_DEBUG "-g3" CACHE STRING ${CONFIG_DOC_STRING} FORCE) | 
|  | 57 set(CMAKE_CXX_FLAGS_DEBUG "-g3" CACHE STRING ${CONFIG_DOC_STRING} FORCE) | 
|  | 58 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g3" CACHE STRING ${CONFIG_DOC_STRING} FORCE) | 
|  | 59 # DebugTrace | 
|  | 60 set(CMAKE_C_FLAGS_DEBUGTRACE "-g3 --tracing" CACHE STRING ${CONFIG_DOC_STRING} F
     ORCE) | 
|  | 61 set(CMAKE_CXX_FLAGS_DEBUGTRACE "-g3 --tracing" CACHE STRING ${CONFIG_DOC_STRING}
      FORCE) | 
|  | 62 set(CMAKE_EXE_LINKER_FLAGS_DEBUGTRACE "-g3 --tracing" CACHE STRING ${CONFIG_DOC_
     STRING} FORCE) | 
| 6 | 63 | 
| 7 # | 64 # | 
| 8 # Sanity check that the tool chain is set up correctly. | 65 # Sanity check that the tool chain is set up correctly. | 
| 9 # | 66 # | 
| 10 if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") | 67 if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") | 
| 11   message(FATAL_ERROR | 68   message(FATAL_ERROR | 
| 12           "The Emscripten toolchain does not appear is not installed in the" | 69           "The Emscripten toolchain does not appear is not installed in the" | 
| 13           "CMake cache. Please follow the instructions in README.CMAKE.") | 70           "CMake cache. Please follow the instructions in README.CMAKE.") | 
| 14 endif() | 71 endif() | 
| 15 | 72 | 
| 16 # | 73 # | 
| 17 # Node.js is required to generate the bindings file. | 74 # Node.js is required to generate the bindings file. | 
| 18 # | 75 # | 
| 19 if (${NODE_JS_EXECUTABLE} MATCHES "NOTFOUND") | 76 if(${NODE_JS_EXECUTABLE} MATCHES "NOTFOUND") | 
| 20   message(FATAL_ERROR | 77   message(FATAL_ERROR | 
| 21           "The Node interpreter was not found while setting up the Emscripten" | 78           "The Node interpreter was not found while setting up the Emscripten" | 
| 22           "toolchain.") | 79           "toolchain.") | 
| 23 endif() | 80 endif() | 
| 24 | 81 | 
| 25 # | 82 # | 
| 26 # Legacy options from `compile` script | 83 # Source file lists | 
| 27 # TODO: convert these to configurations | 84 #   We don't use globbing. CMakes expands a globbing expression at the time it | 
| 28 # | 85 #   runs, not at the time the build runs. Thus if we were to use globbing, we | 
| 29 option(debug "Disable code minification") | 86 #   would generate build files with fixed lists of sources found at the time | 
| 30 option(tracing "Enable memory tracing") | 87 #   CMake runs. Afterwards, such a build would not discover new files created | 
|  | 88 #   in the interim. | 
|  | 89 # | 
|  | 90 set(COMMON_SOURCES | 
|  | 91     compiled/bindings/runtime_utils.cpp | 
|  | 92     compiled/filter/ActiveFilter.cpp | 
|  | 93     compiled/filter/BlockingFilter.cpp | 
|  | 94     compiled/filter/CommentFilter.cpp | 
|  | 95     compiled/filter/ElemHideBase.cpp | 
|  | 96     compiled/filter/ElemHideEmulationFilter.cpp | 
|  | 97     compiled/filter/ElemHideException.cpp | 
|  | 98     compiled/filter/ElemHideFilter.cpp | 
|  | 99     compiled/filter/Filter.cpp | 
|  | 100     compiled/filter/InvalidFilter.cpp | 
|  | 101     compiled/filter/RegExpFilter.cpp | 
|  | 102     compiled/filter/WhitelistFilter.cpp | 
|  | 103     compiled/storage/FilterStorage.cpp | 
|  | 104     compiled/subscription/DownloadableSubscription.cpp | 
|  | 105     compiled/subscription/Subscription.cpp | 
|  | 106     compiled/subscription/UserDefinedSubscription.cpp | 
|  | 107     ) | 
|  | 108 set(CORE_SPECIFIC_SOURCES | 
|  | 109     compiled/traceInit.cpp | 
|  | 110     ) | 
|  | 111 set(BINDINGS_SPECIFIC_SOURCES | 
|  | 112     compiled/bindings/generator.cpp | 
|  | 113     compiled/bindings/main.cpp | 
|  | 114     ) | 
|  | 115 | 
|  | 116 set(BINDINGS_GENERATOR_SOURCES ${COMMON_SOURCES}) | 
|  | 117 list(APPEND BINDINGS_GENERATOR_SOURCES ${BINDINGS_SPECIFIC_SOURCES}) | 
|  | 118 list(SORT BINDINGS_GENERATOR_SOURCES) | 
| 31 | 119 | 
| 32 # | 120 set(CORE_SOURCES ${COMMON_SOURCES}) | 
| 33 # Source file lists | 121 list(APPEND CORE_SOURCES ${CORE_SPECIFIC_SOURCES}) | 
| 34 # | 122 list(SORT CORE_SOURCES) | 
| 35 file(GLOB_RECURSE ALL_SOURCES compiled/*.cpp) |  | 
| 36 # Sort, so that the same source code always yields identical results. |  | 
| 37 list(SORT ALL_SOURCES) |  | 
| 38 set(BINDINGS_GENERATOR_SOURCES ${ALL_SOURCES}) |  | 
| 39 set(CORE_SOURCES ${ALL_SOURCES}) |  | 
| 40 list(FILTER CORE_SOURCES EXCLUDE REGEX compiled/bindings/runtime_) |  | 
| 41 | 123 | 
| 42 # | 124 # | 
| 43 # Options for all targets | 125 # Options for all targets | 
| 44 # | 126 # | 
| 45 add_compile_options(-std=c++1z) | 127 add_compile_options(-std=c++1z) | 
| 46 | 128 | 
| 47 # | 129 # | 
| 48 # Target: bindings-generator | 130 # Target: bindings-generator | 
| 49 # | 131 # | 
| 50 add_executable(bindings-generator ${BINDINGS_GENERATOR_SOURCES}) | 132 add_executable(bindings-generator ${BINDINGS_GENERATOR_SOURCES}) | 
|  | 133 # | 
|  | 134 # CMake overloads `target_link_libraries` for both libraries (as named) and | 
|  | 135 # for linker flags (very much not as named). In order for CMake to recognize | 
|  | 136 # an option with parameter, that is, with an embedded space, it requires that | 
|  | 137 # it be quoted. This oddity is not well-documented in CMake. | 
|  | 138 # | 
| 51 target_link_libraries(bindings-generator | 139 target_link_libraries(bindings-generator | 
| 52                       "--js-library compiled/library.js" | 140                       "--js-library ${PROJECT_SOURCE_DIR}/compiled/library.js" | 
| 53                       "--js-library compiled/bindings/library.js") | 141                       "--js-library ${PROJECT_SOURCE_DIR}/compiled/bindings/libr
     ary.js" | 
|  | 142                       ) | 
| 54 | 143 | 
| 55 # | 144 # | 
| 56 # Target: bindings | 145 # File: bindings.js | 
| 57 # | 146 # | 
| 58 add_custom_target(bindings | 147 # CMake does not consider bindings.js to be a "top-level target". Rather it's | 
|  | 148 # considered a "file-level" entity. Thus it's added to a source list instead | 
|  | 149 # of using it to declare a dependency. | 
|  | 150 # | 
|  | 151 add_custom_command(OUTPUT bindings.js | 
| 59                   COMMAND ${NODE_JS_EXECUTABLE} bindings-generator >bindings.js | 152                   COMMAND ${NODE_JS_EXECUTABLE} bindings-generator >bindings.js | 
|  | 153                   DEPENDS bindings-generator | 
| 60                   BYPRODUCTS bindings.js) | 154                   BYPRODUCTS bindings.js) | 
| 61 add_dependencies(bindings bindings-generator) |  | 
| 62 |  | 
| 63 | 155 | 
| 64 # | 156 # | 
| 65 # Target: Core | 157 # Target: Core | 
| 66 # | 158 # | 
| 67 add_executable(compiled ${CORE_SOURCES}) | 159 add_executable(compiled ${CORE_SOURCES} bindings.js) | 
| 68 add_dependencies(compiled bindings) |  | 
| 69 |  | 
| 70 target_compile_options(compiled PRIVATE | 160 target_compile_options(compiled PRIVATE | 
| 71                        -O3 -m32 | 161                        -O3 -m32 -Wall -Werror -fno-rtti -fno-exceptions) | 
| 72                        --memory-init-file 0 --emit-symbol-map | 162 target_link_libraries(compiled PRIVATE | 
| 73                        -Wall -Werror -fno-rtti) | 163                       "--post-js bindings.js" | 
| 74 # Disabled. This option is present in the compile script (see its script | 164                       -O3 | 
| 75 # variable ADDITIONAL_PARAMS). When present as a proper compile option, | 165                       --emit-symbol-map | 
| 76 # however, it yields an error "cannot use 'throw' with exceptions disabled" | 166                       "--memory-init-file 0" | 
| 77 # when added as a proper compile option. | 167                       "-s ASM_JS=2" | 
| 78 #target_compile_options(compiled PRIVATE -fno-exceptions) | 168                       "-s TOTAL_MEMORY=16*1024*1024" | 
| 79 | 169                       "-s TOTAL_STACK=1*1024*1024" | 
| 80 | 170                       "-s ALLOW_MEMORY_GROWTH=1" | 
| 81 # | 171                       "-s NO_EXIT_RUNTIME=1" | 
| 82 # CMake overloads `target_link_libraries` for both libraries (as stated) and | 172                       "-s NO_DYNAMIC_EXECUTION=1" | 
| 83 # for linker flags (very much not as a stated). In order for CMake to | 173                       "-s NO_FILESYSTEM=1" | 
| 84 # recognize an option with parameter, that is, with an embedded space, it | 174                       "-s INVOKE_RUN=0" | 
| 85 # requires that it be quoted. | 175                       "-s TEXTDECODER=0" | 
| 86 # | 176                       "-s EXPORTED_RUNTIME_METHODS=\"['cwrap','ccall','stringToA
     scii']\"" | 
| 87 target_link_libraries(compiled PRIVATE "--post-js bindings.js") | 177                       "--js-library ${PROJECT_SOURCE_DIR}/compiled/library.js" | 
| 88 # | 178                       ) | 
| 89 # The interaction between command line parsing of "-s SHELL_FILE=" and Windows | 179 # Use an absolute, native path for the SHELL_FILE argument | 
| 90 # path names with backslashes is, frankly, insane. It's left as an exercise, |  | 
| 91 # that is, waste of time, for the reader to divine all the levels of quoting |  | 
| 92 # required to make it work. |  | 
| 93 # |  | 
| 94 file(GLOB SHELL_FILE compiled/shell.js) | 180 file(GLOB SHELL_FILE compiled/shell.js) | 
| 95 file(TO_NATIVE_PATH ${SHELL_FILE} SHELL_FILE) | 181 file(TO_NATIVE_PATH ${SHELL_FILE} SHELL_FILE) | 
| 96 target_link_libraries(compiled PRIVATE "-s SHELL_FILE=\"\\\"${SHELL_FILE}\\\"\""
     ) | 182 target_link_libraries(compiled PRIVATE "-s SHELL_FILE=\"'${SHELL_FILE}'\"") | 
| 97 target_link_libraries(compiled PRIVATE "-s ASM_JS=2") |  | 
| 98 target_link_libraries(compiled PRIVATE "-s TOTAL_MEMORY=16*1024*1024") |  | 
| 99 target_link_libraries(compiled PRIVATE "-s TOTAL_STACK=1*1024*1024") |  | 
| 100 target_link_libraries(compiled PRIVATE "-s ALLOW_MEMORY_GROWTH=1") |  | 
| 101 target_link_libraries(compiled PRIVATE "-s NO_EXIT_RUNTIME=1") |  | 
| 102 target_link_libraries(compiled PRIVATE "-s NO_DYNAMIC_EXECUTION=1") |  | 
| 103 target_link_libraries(compiled PRIVATE "-s NO_FILESYSTEM=1") |  | 
| 104 target_link_libraries(compiled PRIVATE "-s INVOKE_RUN=0") |  | 
| 105 target_link_libraries(compiled PRIVATE "-s TEXTDECODER=0") |  | 
| 106 target_link_libraries(compiled PRIVATE "-s EXPORTED_RUNTIME_METHODS=\"['cwrap','
     ccall','stringToAscii']\"") |  | 
| 107 | 183 | 
| 108 |  | 
| 109 # |  | 
| 110 # GENERAL TODO |  | 
| 111 # |  | 
| 112 # - Bindings and generator are in the root, unlike "compiled/" in the script. |  | 
| 113 # - "compiled.js" is in the root, unlike "lib/" in the script. |  | 
| LEFT | RIGHT | 
|---|