-
Notifications
You must be signed in to change notification settings - Fork 26
/
CMakeLists.txt
80 lines (65 loc) · 2.7 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
cmake_minimum_required(VERSION 3.18)
project(xgrammar LANGUAGES CXX)
if(EXISTS ${CMAKE_BINARY_DIR}/config.cmake)
message(STATUS "Config file: ${CMAKE_BINARY_DIR}/config.cmake")
include(${CMAKE_BINARY_DIR}/config.cmake)
elseif(EXISTS ${PROJECT_SOURCE_DIR}/config.cmake)
message(STATUS "Config file: ${PROJECT_SOURCE_DIR}/config.cmake")
include(${PROJECT_SOURCE_DIR}/config.cmake)
elseif(EXISTS ${PROJECT_SOURCE_DIR}/cmake/config.cmake)
message(STATUS "Config file: ${PROJECT_SOURCE_DIR}/cmake/config.cmake")
include(${PROJECT_SOURCE_DIR}/cmake/config.cmake)
else()
message(STATUS "No config.cmake found. Using the default config")
endif()
option(XGRAMMAR_BUILD_PYTHON_BINDINGS "Build Python bindings" ON)
option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" ON)
set(XGRAMMAR_CUDA_ARCHITECTURES
native
CACHE STRING "CUDA architectures"
)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type specified; defaulting to CMAKE_BUILD_TYPE=RelWithDebugInfo.")
set(CMAKE_BUILD_TYPE
"RelWithDebugInfo"
CACHE STRING "The build type" FORCE
)
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Build Python bindings: ${XGRAMMAR_BUILD_PYTHON_BINDINGS}")
message(STATUS "Build C++ tests: ${XGRAMMAR_BUILD_CXX_TESTS}")
message(STATUS "CUDA architectures: ${XGRAMMAR_CUDA_ARCHITECTURES}")
if(MSVC)
set(CMAKE_CXX_FLAGS "/Wall ${CMAKE_CXX_FLAGS}")
else()
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "-Ofast ${CMAKE_CXX_FLAGS}")
endif()
set(CMAKE_CXX_FLAGS
"-Wall -Wextra -Werror -Wno-pedantic -Wno-unused-parameter -flto=auto ${CMAKE_CXX_FLAGS}"
)
endif()
set(XGRAMMAR_INCLUDE_PATH
${PROJECT_SOURCE_DIR}/3rdparty/picojson ${PROJECT_SOURCE_DIR}/3rdparty/dlpack/include
${PROJECT_SOURCE_DIR}/include
)
file(GLOB_RECURSE XGRAMMAR_SOURCES_PATH "${PROJECT_SOURCE_DIR}/cpp/*.cc")
list(FILTER XGRAMMAR_SOURCES_PATH EXCLUDE REGEX "${PROJECT_SOURCE_DIR}/cpp/pybind/.*\\.cc")
add_library(xgrammar STATIC ${XGRAMMAR_SOURCES_PATH})
target_include_directories(xgrammar PUBLIC ${XGRAMMAR_INCLUDE_PATH})
if(XGRAMMAR_BUILD_PYTHON_BINDINGS)
add_subdirectory(${PROJECT_SOURCE_DIR}/cpp/pybind)
endif()
if(XGRAMMAR_BUILD_CXX_TESTS)
add_subdirectory(${PROJECT_SOURCE_DIR}/3rdparty/googletest)
file(GLOB_RECURSE XGRAMMAR_TEST_SOURCES_PATH "${PROJECT_SOURCE_DIR}/tests/cpp/*.cc")
enable_testing()
add_executable(xgrammar_test ${XGRAMMAR_TEST_SOURCES_PATH})
target_include_directories(xgrammar_test PUBLIC ${PROJECT_SOURCE_DIR}/cpp)
target_link_libraries(xgrammar_test xgrammar gtest gtest_main)
include(GoogleTest)
gtest_discover_tests(xgrammar_test)
endif()