blob: 919d71d8f04f6e16534fe6ac96d36633f8b7c13a [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 Tangf72e81e2022-08-24 10:05:57 +010056add_library(cper-generate STATIC
Lawrence Tangd34f2b12022-07-19 15:36:31 +010057 generator/cper-generate.c
58 generator/gen-utils.c
Lawrence Tangaacf0e22022-07-20 13:28:52 +010059 common-utils.c
Lawrence Tangd34f2b12022-07-19 15:36:31 +010060 ${GeneratorSectionSources}
61 ${EDKSources}
62)
Lawrence Tangf72e81e2022-08-24 10:05:57 +010063add_executable(cper-convert cli-app/cper-convert.c)
64add_executable(cper-generate-cli
65 generator/cper-generate-cli.c
66 ${EDKSources}
67)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010068add_executable(cper-tests
69 tests/ir-tests.cpp
70 tests/test-utils.cpp
71 generator/cper-generate.c
72 generator/gen-utils.c
Lawrence Tang580423f2022-08-24 09:37:53 +010073 sections/cper-section.c
Lawrence Tangd34f2b12022-07-19 15:36:31 +010074 ${GeneratorSectionSources}
75 ${EDKSources}
76)
Lawrence Tang1b0b00e2022-07-05 10:33:10 +010077
Lawrence Tangf72e81e2022-08-24 10:05:57 +010078# Linking, compile & property setup for targets.
Lawrence Tangd34f2b12022-07-19 15:36:31 +010079target_link_libraries(cper-tests cper-parse json-c GTest::gtest_main)
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010080target_link_libraries(cper-parse json-c b64c)
81target_link_libraries(cper-convert cper-parse)
Lawrence Tangf72e81e2022-08-24 10:05:57 +010082target_link_libraries(cper-generate-cli cper-generate)
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010083target_compile_options(cper-parse PRIVATE -Wno-address-of-packed-member)
Lawrence Tang580423f2022-08-24 09:37:53 +010084target_compile_options(cper-tests PRIVATE -fpermissive)
Lawrence Tangf72e81e2022-08-24 10:05:57 +010085set_target_properties(cper-generate-cli PROPERTIES OUTPUT_NAME "cper-generate")
Lawrence Tangb8fa2f72022-07-18 10:50:19 +010086
87# Copy required specification JSON for command line application.
88add_custom_command(TARGET cper-convert POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory bin/specification)
Lawrence Tangd34f2b12022-07-19 15:36:31 +010089add_custom_command(TARGET cper-convert POST_BUILD COMMAND cp -r specification/json/* bin/specification)
90
91# Add tests to GoogleTest.
Lawrence Tang12042d72022-07-21 13:34:13 +010092gtest_discover_tests(cper-tests WORKING_DIRECTORY bin/)
93
94# Generate Python bindings with SWIG.
Lawrence Tang2ca29e52022-08-09 09:29:56 +010095if (NOT CMAKE_CROSSCOMPILING)
96 include_directories(cperparse_pylib ${PYTHON_INCLUDE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
97 swig_add_library(cperparse_pylib
98 TYPE STATIC
99 LANGUAGE python
100 SOURCES cper-parse.i
101 OUTPUT_DIR lib/
102 )
103 swig_link_libraries(cperparse_pylib
104 cper-parse
105 json-c
106 ${PYTHON_LIBRARIES}
107 )
108endif()