blob: 0600df902607f4383203a89d9e197a0b43fb1f42 [file] [log] [blame]
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01001cmake_minimum_required(VERSION 3.10)
Lawrence Tang4d106982022-08-08 11:46:48 +01002project(CPERParse)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +01003include(FetchContent)
Lawrence Tangd34f2b12022-07-19 15:36:31 +01004include(GoogleTest)
Lawrence Tang2ca29e52022-08-09 09:29:56 +01005if (NOT CMAKE_CROSSCOMPILING)
6 include(FindSWIG)
7 include(UseSWIG)
8 find_package(PythonLibs 3 REQUIRED)
9 find_package(PythonInterp ${PYTHONLIBS_VERSION_STRING} REQUIRED)
10endif()
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010011
Lawrence Tangd34f2b12022-07-19 15:36:31 +010012# GoogleTest requires at least C++14.
13set(CMAKE_CXX_STANDARD 14)
14
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010015# Output into subdirectories /lib and /bin.
16set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
17set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
18set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
19set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
20
21# Fetch json-c from git repository.
22FetchContent_Declare(
23 json-c
24 GIT_REPOSITORY https://github.com/json-c/json-c.git
Lawrence Tang9aaf8352022-08-08 09:53:39 +010025 GIT_TAG 9021cdcdd01fc9dbcbe1f06391848c2ac915212f # 0.15
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010026)
Lawrence Tangd7e8ca32022-07-07 10:25:53 +010027
28# Fetch b64-c from git repository.
29FetchContent_Declare(
30 b64-c
31 GIT_REPOSITORY https://github.com/jwerle/b64.c.git
32 GIT_TAG c33188cd541f19b072ee4988d8224ea6c964bed1 # 9/2/2021
33)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010034
35# Fetch GoogleTest from git repository.
36FetchContent_Declare(
37 googletest
38 GIT_REPOSITORY https://github.com/google/googletest.git
39 GIT_TAG release-1.12.1
40)
41FetchContent_MakeAvailable(json-c b64-c googletest)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010042
43# Add library and test executable.
44file(GLOB SectionSources sections/*.c)
45file(GLOB EDKSources edk/*.c)
Lawrence Tang02c801a2022-07-18 14:43:52 +010046file(GLOB GeneratorSectionSources generator/sections/*.c)
Lawrence Tangaacf0e22022-07-20 13:28:52 +010047add_library(cper-parse STATIC
48 cper-parse.c
49 ir-parse.c
50 cper-utils.c
51 common-utils.c
52 json-schema.c
53 ${SectionSources}
54 ${EDKSources}
55)
Lawrence Tang02c801a2022-07-18 14:43:52 +010056add_executable(cper-convert cli-app/cper-convert.c)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010057add_executable(cper-generate
58 generator/cper-generate-cli.c
59 generator/cper-generate.c
60 generator/gen-utils.c
Lawrence Tangaacf0e22022-07-20 13:28:52 +010061 common-utils.c
Lawrence Tangd34f2b12022-07-19 15:36:31 +010062 ${GeneratorSectionSources}
63 ${EDKSources}
64)
65add_executable(cper-tests
66 tests/ir-tests.cpp
67 tests/test-utils.cpp
68 generator/cper-generate.c
69 generator/gen-utils.c
70 ${GeneratorSectionSources}
71 ${EDKSources}
72)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010073
74# Link library.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010075target_link_libraries(cper-tests cper-parse json-c GTest::gtest_main)
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010076target_link_libraries(cper-parse json-c b64c)
77target_link_libraries(cper-convert cper-parse)
78target_compile_options(cper-parse PRIVATE -Wno-address-of-packed-member)
79
80# Copy required specification JSON for command line application.
81add_custom_command(TARGET cper-convert POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory bin/specification)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010082add_custom_command(TARGET cper-convert POST_BUILD COMMAND cp -r specification/json/* bin/specification)
83
84# Add tests to GoogleTest.
Lawrence Tang12042d72022-07-21 13:34:13 +010085gtest_discover_tests(cper-tests WORKING_DIRECTORY bin/)
86
87# Generate Python bindings with SWIG.
Lawrence Tang2ca29e52022-08-09 09:29:56 +010088if (NOT CMAKE_CROSSCOMPILING)
89 include_directories(cperparse_pylib ${PYTHON_INCLUDE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
90 swig_add_library(cperparse_pylib
91 TYPE STATIC
92 LANGUAGE python
93 SOURCES cper-parse.i
94 OUTPUT_DIR lib/
95 )
96 swig_link_libraries(cperparse_pylib
97 cper-parse
98 json-c
99 ${PYTHON_LIBRARIES}
100 )
101endif()