Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 1 | # Copyright 2017 The NXT Authors |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | cmake_minimum_required(VERSION 2.8) |
| 16 | project(nxt C CXX) |
| 17 | |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 18 | if(NOT CMAKE_BUILD_TYPE) |
| 19 | set(CMAKE_BUILD_TYPE "Debug" CACHE STRING |
| 20 | "Build type (Debug, Release, RelWithDebInfo, MinSizeRel)" FORCE) |
| 21 | endif() |
| 22 | |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 23 | ################################################################################ |
| 24 | # Configuration options |
| 25 | ################################################################################ |
| 26 | |
| 27 | option(NXT_USE_WERROR "Treat warnings as error (useful for CI)" 0) |
| 28 | |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 29 | # Default values for the backend-enabling options |
Corentin Wallez | 28684d9 | 2017-07-14 09:53:59 -0400 | [diff] [blame] | 30 | set(ENABLE_D3D12 OFF) |
| 31 | set(ENABLE_METAL OFF) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 32 | if (WIN32) |
Corentin Wallez | 28684d9 | 2017-07-14 09:53:59 -0400 | [diff] [blame] | 33 | set(ENABLE_D3D12 ON) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 34 | elseif(APPLE) |
Corentin Wallez | 28684d9 | 2017-07-14 09:53:59 -0400 | [diff] [blame] | 35 | set(ENABLE_METAL ON) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 36 | endif() |
| 37 | |
Corentin Wallez | 28684d9 | 2017-07-14 09:53:59 -0400 | [diff] [blame] | 38 | option(NXT_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12}) |
| 39 | option(NXT_ENABLE_METAL "Enable compilation of the Metal backend" ${ENABLE_METAL}) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 40 | option(NXT_ENABLE_NULL "Enable compilation of the Null backend" ON) |
| 41 | option(NXT_ENABLE_OPENGL "Enable compilation of the OpenGL backend" ON) |
| 42 | option(NXT_ENABLE_VULKAN "Enable compilation of the Vulkan backend" OFF) |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 43 | option(NXT_ALWAYS_ASSERT "Enable assertions on all build types" OFF) |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 44 | |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 45 | ################################################################################ |
| 46 | # Precompute compile flags and defines, functions to set them |
| 47 | ################################################################################ |
| 48 | |
| 49 | set(NXT_FLAGS "") |
| 50 | set(NXT_DEFS "") |
| 51 | set(NXT_INTERNAL_FLAGS "") |
| 52 | set(NXT_INTERNAL_DEFS "") |
Corentin Wallez | 0f833f3 | 2017-07-10 20:09:59 -0400 | [diff] [blame] | 53 | set(NXT_GENERATED_FLAGS "") |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 54 | |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 55 | set(NXT_ENABLE_ASSERTS $<OR:$<CONFIG:Debug>,$<BOOL:${NXT_ALWAYS_ASSERT}>>) |
| 56 | |
| 57 | list(APPEND NXT_DEFS $<${NXT_ENABLE_ASSERTS}:NXT_ENABLE_ASSERTS>) |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 58 | |
Corentin Wallez | 275817a | 2017-07-12 12:43:24 -0400 | [diff] [blame] | 59 | if (NXT_ENABLE_D3D12) |
| 60 | list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_D3D12") |
| 61 | endif() |
| 62 | if (NXT_ENABLE_METAL) |
| 63 | list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_METAL") |
| 64 | endif() |
| 65 | if (NXT_ENABLE_NULL) |
| 66 | list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_NULL") |
| 67 | endif() |
| 68 | if (NXT_ENABLE_OPENGL) |
| 69 | list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_OPENGL") |
| 70 | endif() |
| 71 | if (NXT_ENABLE_VULKAN) |
| 72 | list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_VULKAN") |
| 73 | endif() |
| 74 | |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 75 | if (WIN32) |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 76 | # Define NOMINMAX to prevent conflicts between std::min/max and the min/max macros in WinDef.h |
| 77 | list(APPEND NXT_DEFS "NOMINMAX") |
Corentin Wallez | 4db6327 | 2017-07-12 12:55:15 -0400 | [diff] [blame] | 78 | # Avoid Windows.h including a lot of headers |
| 79 | list(APPEND NXT_DEFS "WIN32_LEAN_AND_MEAN") |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 80 | # Remove compile error where the mock NXT creates too many sections for the old obj format. |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 81 | list(APPEND NXT_FLAGS "/bigobj") |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 82 | endif() |
| 83 | |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 84 | if (MSVC) |
| 85 | list(APPEND NXT_FLAGS "/std:c++14") |
Corentin Wallez | 0f833f3 | 2017-07-10 20:09:59 -0400 | [diff] [blame] | 86 | list(APPEND NXT_INTERNAL_FLAGS "/W4") |
| 87 | # Allow declarations hiding members as it is used all over NXT |
| 88 | list(APPEND NXT_INTERNAL_FLAGS "/wd4458") |
| 89 | list(APPEND NXT_INTERNAL_FLAGS "/wd4996") # Allow deprecated functions like strncpy |
| 90 | |
| 91 | list(APPEND NXT_GENERATED_FLAGS "/wd4702") # Allow unreachable code |
| 92 | list(APPEND NXT_GENERATED_FLAGS "/wd4189") # Allow unused variable |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 93 | |
Corentin Wallez | 0f833f3 | 2017-07-10 20:09:59 -0400 | [diff] [blame] | 94 | if(NXT_USE_WERROR) |
| 95 | list(APPEND NXT_INTERNAL_FLAGS "/WX") |
| 96 | endif() |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 97 | else() |
| 98 | # Activate C++14 only on C++ files, not C files. |
| 99 | list(APPEND NXT_FLAGS "$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-std=c++14>") |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 100 | # enable -Wold-style-cast on C++ |
| 101 | list(APPEND NXT_FLAGS "$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-Wold-style-cast>") |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 102 | list(APPEND NXT_FLAGS "-fPIC") |
| 103 | |
| 104 | list(APPEND NXT_INTERNAL_FLAGS "-Wall" "-Wextra") |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 105 | list(APPEND NXT_INTERNAL_FLAGS "-pedantic") |
Corentin Wallez | 0f833f3 | 2017-07-10 20:09:59 -0400 | [diff] [blame] | 106 | list(APPEND NXT_GENERATED_FLAGS "-Wno-unused-variable" "-Wno-unused-function") |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 107 | |
| 108 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") |
| 109 | # don't break the build on older clang versions |
| 110 | list(APPEND NXT_INTERNAL_FLAGS "-Wno-error=unknown-warning-option") |
| 111 | # GCC's conversion warnings are less useful than clang's |
| 112 | list(APPEND NXT_INTERNAL_FLAGS "-Wconversion" "-Wno-sign-conversion") |
| 113 | # disable a clang-only -pedantic warning |
| 114 | list(APPEND NXT_INTERNAL_FLAGS "-Wno-gnu-zero-variadic-macro-arguments") |
| 115 | # additional potentially useful warnings (feel free to remove if they prove un-useful) |
| 116 | list(APPEND NXT_INTERNAL_FLAGS "-Wextra-semi") |
| 117 | list(APPEND NXT_INTERNAL_FLAGS "-Wstrict-aliasing") |
| 118 | list(APPEND NXT_INTERNAL_FLAGS "-Wunreachable-code") |
| 119 | list(APPEND NXT_GENERATED_FLAGS "-Wno-unreachable-code") |
| 120 | # Probably okay to enable if we establish a field naming convention: |
| 121 | #list(APPEND NXT_INTERNAL_FLAGS "-Wshadow") |
| 122 | endif() |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 123 | if(NXT_USE_WERROR) |
| 124 | list(APPEND NXT_INTERNAL_FLAGS "-Werror") |
| 125 | endif() |
| 126 | endif() |
| 127 | |
| 128 | function(NXTExternalTarget folder target) |
| 129 | set_property(TARGET ${target} APPEND PROPERTY COMPILE_OPTIONS ${NXT_FLAGS}) |
| 130 | set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS ${NXT_DEFS}) |
Corentin Wallez | 6fb3aeb | 2017-07-10 19:08:46 -0400 | [diff] [blame] | 131 | set_property(TARGET ${target} PROPERTY FOLDER "nxt/${folder}") |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 132 | endfunction() |
| 133 | |
| 134 | function(NXTInternalTarget folder target) |
| 135 | NXTExternalTarget("${folder}" ${target}) |
| 136 | set_property(TARGET ${target} APPEND PROPERTY COMPILE_OPTIONS ${NXT_INTERNAL_FLAGS}) |
| 137 | set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS ${NXT_INTERNAL_DEFS}) |
Corentin Wallez | 3df6441 | 2017-07-10 22:39:04 -0400 | [diff] [blame] | 138 | |
| 139 | # Group the target sources by folder to have folders show in Visual Studio |
| 140 | if (MSVC) |
| 141 | get_target_property(targetSources ${target} SOURCES) |
| 142 | foreach(sourceFile IN ITEMS ${targetSources}) |
| 143 | if (IS_ABSOLUTE "${sourceFile}") |
| 144 | file(RELATIVE_PATH sourceFile "${CMAKE_CURRENT_SOURCE_DIR}" "${sourceFile}") |
| 145 | endif() |
| 146 | get_filename_component(sourceDir "${sourceFile}" PATH) |
| 147 | string(REPLACE "/" "\\" sourceDir "${sourceDir}") |
| 148 | source_group("${sourceDir}" FILES "${sourceFile}") |
| 149 | endforeach() |
| 150 | endif() |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 151 | endfunction() |
| 152 | |
Corentin Wallez | 3df6441 | 2017-07-10 22:39:04 -0400 | [diff] [blame] | 153 | # Enable the creation of folders for Visual Studio projects |
| 154 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 155 | |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 156 | ################################################################################ |
| 157 | # Generate the C and C++ NXT APIs |
| 158 | ################################################################################ |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 159 | |
| 160 | set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/include) |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 161 | set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) |
| 162 | |
| 163 | add_subdirectory(generator) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 164 | |
| 165 | Generate( |
| 166 | LIB_NAME nxt |
Corentin Wallez | 26275d0 | 2017-05-29 11:21:33 -0700 | [diff] [blame] | 167 | LIB_TYPE STATIC |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 168 | PRINT_NAME libNXT |
| 169 | COMMAND_LINE_ARGS |
| 170 | ${GENERATOR_COMMON_ARGS} |
| 171 | -T nxt |
| 172 | ) |
| 173 | target_include_directories(nxt PUBLIC ${GENERATED_DIR}) |
| 174 | |
| 175 | Generate( |
| 176 | LIB_NAME nxtcpp |
Corentin Wallez | 26275d0 | 2017-05-29 11:21:33 -0700 | [diff] [blame] | 177 | LIB_TYPE STATIC |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 178 | PRINT_NAME libNXT++ |
| 179 | COMMAND_LINE_ARGS |
| 180 | ${GENERATOR_COMMON_ARGS} |
| 181 | -T nxtcpp |
| 182 | ) |
| 183 | target_include_directories(nxtcpp PUBLIC ${GENERATED_DIR} PUBLIC ${INCLUDE_DIR}) |
| 184 | target_link_libraries(nxtcpp nxt) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 185 | |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 186 | ################################################################################ |
| 187 | # Call to other CMakeLists.txt |
| 188 | ################################################################################ |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 189 | |
Corentin Wallez | bd0594b | 2017-07-07 14:41:17 -0400 | [diff] [blame] | 190 | add_subdirectory(third_party) |
Corentin Wallez | 0a58812 | 2017-05-16 20:16:56 +0200 | [diff] [blame] | 191 | |
Corentin Wallez | fffe6df | 2017-07-06 14:41:13 -0400 | [diff] [blame] | 192 | add_subdirectory(src/common) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 193 | add_subdirectory(src/backend) |
| 194 | add_subdirectory(src/wire) |
Corentin Wallez | 1bd219d | 2017-06-19 12:53:38 -0400 | [diff] [blame] | 195 | add_subdirectory(src/utils) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 196 | add_subdirectory(src/tests) |
| 197 | |
| 198 | add_subdirectory(examples) |