blob: 145f9e8c7f98a34e85e086355b1efba6d2071782 [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001# 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
15cmake_minimum_required(VERSION 2.8)
16project(nxt C CXX)
17
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070018if(NOT CMAKE_BUILD_TYPE)
19 set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
20 "Build type (Debug, Release, RelWithDebInfo, MinSizeRel)" FORCE)
21endif()
22
Corentin Wallezbd0594b2017-07-07 14:41:17 -040023################################################################################
24# Configuration options
25################################################################################
26
27option(NXT_USE_WERROR "Treat warnings as error (useful for CI)" 0)
28
Corentin Wallez275817a2017-07-12 12:43:24 -040029# Default values for the backend-enabling options
Corentin Wallez28684d92017-07-14 09:53:59 -040030set(ENABLE_D3D12 OFF)
31set(ENABLE_METAL OFF)
Corentin Wallez275817a2017-07-12 12:43:24 -040032if (WIN32)
Corentin Wallez28684d92017-07-14 09:53:59 -040033 set(ENABLE_D3D12 ON)
Corentin Wallez275817a2017-07-12 12:43:24 -040034elseif(APPLE)
Corentin Wallez28684d92017-07-14 09:53:59 -040035 set(ENABLE_METAL ON)
Corentin Wallez275817a2017-07-12 12:43:24 -040036endif()
37
Corentin Wallez28684d92017-07-14 09:53:59 -040038option(NXT_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12})
39option(NXT_ENABLE_METAL "Enable compilation of the Metal backend" ${ENABLE_METAL})
Corentin Wallez275817a2017-07-12 12:43:24 -040040option(NXT_ENABLE_NULL "Enable compilation of the Null backend" ON)
41option(NXT_ENABLE_OPENGL "Enable compilation of the OpenGL backend" ON)
42option(NXT_ENABLE_VULKAN "Enable compilation of the Vulkan backend" OFF)
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070043option(NXT_ALWAYS_ASSERT "Enable assertions on all build types" OFF)
Corentin Wallez275817a2017-07-12 12:43:24 -040044
Corentin Wallezbd0594b2017-07-07 14:41:17 -040045################################################################################
46# Precompute compile flags and defines, functions to set them
47################################################################################
48
49set(NXT_FLAGS "")
50set(NXT_DEFS "")
51set(NXT_INTERNAL_FLAGS "")
52set(NXT_INTERNAL_DEFS "")
Corentin Wallez0f833f32017-07-10 20:09:59 -040053set(NXT_GENERATED_FLAGS "")
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040054
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070055set(NXT_ENABLE_ASSERTS $<OR:$<CONFIG:Debug>,$<BOOL:${NXT_ALWAYS_ASSERT}>>)
56
57list(APPEND NXT_DEFS $<${NXT_ENABLE_ASSERTS}:NXT_ENABLE_ASSERTS>)
Corentin Wallezfd589f32017-07-10 13:46:05 -040058
Corentin Wallez275817a2017-07-12 12:43:24 -040059if (NXT_ENABLE_D3D12)
60 list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_D3D12")
61endif()
62if (NXT_ENABLE_METAL)
63 list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_METAL")
64endif()
65if (NXT_ENABLE_NULL)
66 list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_NULL")
67endif()
68if (NXT_ENABLE_OPENGL)
69 list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_OPENGL")
70endif()
71if (NXT_ENABLE_VULKAN)
72 list(APPEND NXT_INTERNAL_DEFS "NXT_ENABLE_BACKEND_VULKAN")
73endif()
74
Corentin Wallez944b60f2017-05-29 11:33:33 -070075if (WIN32)
Corentin Wallezbd0594b2017-07-07 14:41:17 -040076 # Define NOMINMAX to prevent conflicts between std::min/max and the min/max macros in WinDef.h
77 list(APPEND NXT_DEFS "NOMINMAX")
Corentin Wallez4db63272017-07-12 12:55:15 -040078 # Avoid Windows.h including a lot of headers
79 list(APPEND NXT_DEFS "WIN32_LEAN_AND_MEAN")
Corentin Wallez944b60f2017-05-29 11:33:33 -070080 # Remove compile error where the mock NXT creates too many sections for the old obj format.
Corentin Wallezbd0594b2017-07-07 14:41:17 -040081 list(APPEND NXT_FLAGS "/bigobj")
Corentin Wallez944b60f2017-05-29 11:33:33 -070082endif()
83
Corentin Wallezbd0594b2017-07-07 14:41:17 -040084if (MSVC)
85 list(APPEND NXT_FLAGS "/std:c++14")
Corentin Wallez0f833f32017-07-10 20:09:59 -040086 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 Ninomiya59dc03f2017-07-20 07:28:00 -070093
Corentin Wallez0f833f32017-07-10 20:09:59 -040094 if(NXT_USE_WERROR)
95 list(APPEND NXT_INTERNAL_FLAGS "/WX")
96 endif()
Corentin Wallezbd0594b2017-07-07 14:41:17 -040097else()
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 Ninomiya78c8b832017-07-21 17:00:22 -0700100 # enable -Wold-style-cast on C++
101 list(APPEND NXT_FLAGS "$<$<STREQUAL:$<TARGET_PROPERTY:LINKER_LANGUAGE>,CXX>:-Wold-style-cast>")
Corentin Wallezbd0594b2017-07-07 14:41:17 -0400102 list(APPEND NXT_FLAGS "-fPIC")
103
104 list(APPEND NXT_INTERNAL_FLAGS "-Wall" "-Wextra")
Kai Ninomiya78c8b832017-07-21 17:00:22 -0700105 list(APPEND NXT_INTERNAL_FLAGS "-pedantic")
Corentin Wallez0f833f32017-07-10 20:09:59 -0400106 list(APPEND NXT_GENERATED_FLAGS "-Wno-unused-variable" "-Wno-unused-function")
Kai Ninomiya78c8b832017-07-21 17:00:22 -0700107
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 Wallezbd0594b2017-07-07 14:41:17 -0400123 if(NXT_USE_WERROR)
124 list(APPEND NXT_INTERNAL_FLAGS "-Werror")
125 endif()
126endif()
127
128function(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 Wallez6fb3aeb2017-07-10 19:08:46 -0400131 set_property(TARGET ${target} PROPERTY FOLDER "nxt/${folder}")
Corentin Wallezbd0594b2017-07-07 14:41:17 -0400132endfunction()
133
134function(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 Wallez3df64412017-07-10 22:39:04 -0400138
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 Wallezbd0594b2017-07-07 14:41:17 -0400151endfunction()
152
Corentin Wallez3df64412017-07-10 22:39:04 -0400153# Enable the creation of folders for Visual Studio projects
154set_property(GLOBAL PROPERTY USE_FOLDERS ON)
155
Corentin Wallezbd0594b2017-07-07 14:41:17 -0400156################################################################################
157# Generate the C and C++ NXT APIs
158################################################################################
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400159
160set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/include)
Corentin Wallezbd0594b2017-07-07 14:41:17 -0400161set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
162
163add_subdirectory(generator)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400164
165Generate(
166 LIB_NAME nxt
Corentin Wallez26275d02017-05-29 11:21:33 -0700167 LIB_TYPE STATIC
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400168 PRINT_NAME libNXT
169 COMMAND_LINE_ARGS
170 ${GENERATOR_COMMON_ARGS}
171 -T nxt
172)
173target_include_directories(nxt PUBLIC ${GENERATED_DIR})
174
175Generate(
176 LIB_NAME nxtcpp
Corentin Wallez26275d02017-05-29 11:21:33 -0700177 LIB_TYPE STATIC
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400178 PRINT_NAME libNXT++
179 COMMAND_LINE_ARGS
180 ${GENERATOR_COMMON_ARGS}
181 -T nxtcpp
182)
183target_include_directories(nxtcpp PUBLIC ${GENERATED_DIR} PUBLIC ${INCLUDE_DIR})
184target_link_libraries(nxtcpp nxt)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400185
Corentin Wallezbd0594b2017-07-07 14:41:17 -0400186################################################################################
187# Call to other CMakeLists.txt
188################################################################################
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400189
Corentin Wallezbd0594b2017-07-07 14:41:17 -0400190add_subdirectory(third_party)
Corentin Wallez0a588122017-05-16 20:16:56 +0200191
Corentin Wallezfffe6df2017-07-06 14:41:13 -0400192add_subdirectory(src/common)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400193add_subdirectory(src/backend)
194add_subdirectory(src/wire)
Corentin Wallez1bd219d2017-06-19 12:53:38 -0400195add_subdirectory(src/utils)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400196add_subdirectory(src/tests)
197
198add_subdirectory(examples)