Skip to content

Commit

Permalink
feat: create build.json with component info
Browse files Browse the repository at this point in the history
  • Loading branch information
LeMollinger committed Aug 13, 2024
1 parent 0c4ed6a commit 5146608
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 272 deletions.
47 changes: 47 additions & 0 deletions cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,37 @@ macro(spl_create_component)
list(APPEND COMPONENT_NAMES ${component_name})
set(COMPONENT_NAMES ${COMPONENT_NAMES} PARENT_SCOPE)

# collect sources for each component in JSON format
if(SOURCES)
# Whitespaces are needed for beautified JSON output
list(JOIN SOURCES "\",\n \"" formatted_json_sources)
set(formatted_json_sources "[\n \"${formatted_json_sources}\"\n ]")
else()
set(formatted_json_sources "[]")
endif()

# collect test sources for each component in JSON format
if(TEST_SOURCES)
# Whitespaces are needed for beautified JSON output
list(JOIN TEST_SOURCES "\",\n \"" formatted_json_test_sources)
set(formatted_json_test_sources "[\n \"${formatted_json_test_sources}\"\n ]")
else()
set(formatted_json_test_sources "[]")
endif()

# Collect all component information for build.json
set(_build_info " {
\"name\": \"${component_name}\",
\"long_name\": \"${CREATE_COMPONENT_LONG_NAME}\",
\"path\": \"${CMAKE_SOURCE_DIR}/${component_path}\",
\"sources\": ${formatted_json_sources},
\"test_sources\": ${formatted_json_test_sources}
}")

# Append the component information to the global build_info list
list(APPEND build_info ${_build_info})
set(build_info ${build_info} PARENT_SCOPE)

# Collect all component information for sphinx documentation
# - We need to keep track of all components and their information to be able to generate the variant reports.
# For the variants reports, one need to loop over all components and generate component variant specific targets.
Expand Down Expand Up @@ -611,3 +642,19 @@ endmacro()
macro(create_component)
spl_create_component(${ARGN})
endmacro()

macro(_spl_create_build_info_file)
# create empty build.json file
set(build_info_file ${CMAKE_CURRENT_BINARY_DIR}/build.json)

# create preformatted JSON strings for each component
# Whitespaces are needed for beautified JSON output
list(JOIN build_info ",\n " formatted_json_build_info)

# add the components to the build.json file
file(WRITE ${build_info_file} "{
\"components\":[
${formatted_json_build_info}
]
}")
endmacro()
1 change: 1 addition & 0 deletions cmake/spl.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function(_spl_hook_end_of_configure)
_spl_coverage_create_overall_report()
_spl_create_docs_target()
_spl_create_reports_target()
_spl_create_build_info_file()
endif(BUILD_KIT STREQUAL test)
endfunction(_spl_hook_end_of_configure)

Expand Down
577 changes: 305 additions & 272 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions tests/cmake/common.cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ set(some_other_file src/some_other_file.c)

# when
spl_add_component(component)
spl_add_component(some_other_component)
set_target_properties(component PROPERTIES LINKER_LANGUAGE C)

# then
if(NOT "component" IN_LIST COMPONENT_NAMES)
message(FATAL_ERROR "Failing Test case: component not found inside ${COMPONENT_NAMES}.")
endif()

# ## test: _spl_create_build_info_file (tests are written in test_cmake.py)#####
_spl_create_build_info_file()
12 changes: 12 additions & 0 deletions tests/cmake/common.cmake/some_other_component/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
spl_add_source(${some_file})
spl_add_source(${some_other_file} COMPILE_OPTIONS "-DTHE_ANSWER=42" "-DSOME_VALUE=7")
spl_add_test_source(test/some_test_file.cc)
spl_add_test_source(test/some_other_test_file.cc)
spl_create_component(LONG_NAME "Some amazing other component with a long name")

_spl_get_absolute_path(expected_other_source_file "${some_other_file}")
set(expected_compile_options "-DTHE_ANSWER=42")
get_source_file_property(source_file_compile_options ${expected_other_source_file} COMPILE_OPTIONS)
if(NOT expected_compile_options IN_LIST source_file_compile_options)
message(FATAL_ERROR "Failing Test case: ${expected_compile_options} not found inside ${source_file_compile_options}.")
endif()
Empty file.
Empty file.
Empty file.
Empty file.
37 changes: 37 additions & 0 deletions tests/cmake/test_cmake.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import subprocess
from pathlib import Path

from tests.utils import TestDir, create_clean_test_dir

Expand Down Expand Up @@ -32,5 +33,41 @@ def run_cmake_unit_test(self, unit_test: str) -> int:
def test_cmake_common_cmake(self):
assert 0 == self.run_cmake_unit_test("common.cmake")

# check if the build.json file was created
assert Path(f"{self.test_workspace}/common.cmake/build.json").exists()

# check if the build.json file has the correct content
script_dir = Path(__file__).parent.resolve().as_posix()
assert (
Path(f"{self.test_workspace}/common.cmake/build.json").read_text()
== f"""{{
"components":[
{{
"name": "component",
"long_name": "",
"path": "{script_dir}/common.cmake/component",
"sources": [
"{script_dir}/common.cmake/component/src/some_file.c",
"{script_dir}/common.cmake/component/src/some_other_file.c"
],
"test_sources": []
}},
{{
"name": "some_other_component",
"long_name": "Some amazing other component with a long name",
"path": "{script_dir}/common.cmake/some_other_component",
"sources": [
"{script_dir}/common.cmake/some_other_component/src/some_file.c",
"{script_dir}/common.cmake/some_other_component/src/some_other_file.c"
],
"test_sources": [
"{script_dir}/common.cmake/some_other_component/test/some_test_file.cc",
"{script_dir}/common.cmake/some_other_component/test/some_other_test_file.cc"
]
}}
]
}}"""
)

def test_cmake_spl_cmake(self):
assert 0 == self.run_cmake_unit_test("spl.cmake")

0 comments on commit 5146608

Please sign in to comment.