Skip to content

Commit

Permalink
Add basic CMake support files to cpp generation.
Browse files Browse the repository at this point in the history
tanaya-mankad committed Jun 17, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 53ca56b commit 5ee1b65
Showing 6 changed files with 91 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dodo.py
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@ def task_generate_cpp_code():
+ [CORE_SCHEMA_PATH, BASE_META_SCHEMA_PATH, Path(SOURCE_PATH, "header_entries.py"), Path(SOURCE_PATH, "cpp_entries.py")],
"targets": [schema.cpp_header_path for schema in example.cpp_schemas]
+ [schema.cpp_source_path for schema in example.cpp_schemas]
+ example.cpp_support_headers(),
+ example.cpp_support_headers() + [example.cpp_output_dir / "CMakeLists.txt", example.cpp_output_dir / "src" / "CMakeLists.txt"],
"actions": [(example.generate_cpp_headers, [])],
"clean": True,
}
25 changes: 22 additions & 3 deletions lattice/cpp/generate_support_headers.py
Original file line number Diff line number Diff line change
@@ -15,9 +15,28 @@ def support_header_pathnames(output_directory: Path):

def generate_support_headers(namespace_name: str, root_data_groups: list[str], output_directory: Path):
for template in Path(__file__).with_name("templates").iterdir():
enum_info = Template(template.read_text())
generated_file_name = "-".join(snake_style(template.stem).split("_"))
if ".h" in template.suffixes:
enum_info = Template(template.read_text())
generated_file_name = "-".join(snake_style(template.stem).split("_"))
dump(
enum_info.render(namespace=namespace_name, root_objects=root_data_groups),
Path(output_directory) / generated_file_name,
)

def generate_build_support(project_name: str, output_directory: Path):
generated_file_name = "CMakeLists.txt"

project_cmake_file = Path(__file__).with_name("templates") / "project-cmake.txt.j2"
if project_cmake_file.exists():
enum_info = Template(project_cmake_file.read_text())
dump(
enum_info.render(namespace=namespace_name, root_objects=root_data_groups),
enum_info.render(project_name=project_name),
Path(output_directory) / generated_file_name,
)
src_cmake_file = Path(__file__).with_name("templates") / "project-src-cmake.txt.j2"
if src_cmake_file.exists():
enum_info = Template(src_cmake_file.read_text())
dump(
enum_info.render(project_name=project_name),
Path(output_directory) / "src" / generated_file_name,
)
31 changes: 31 additions & 0 deletions lattice/cpp/templates/project-cmake.txt.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required (VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project({{project_name}})

# Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()

find_package(Git QUIET)

include(CTest)
set(JSON_BuildTests OFF CACHE INTERNAL "")

option(${PROJECT_NAME}_BUILD_TESTING "Build ${PROJECT_NAME} testing targets" OFF)
option(${PROJECT_NAME}_COVERAGE "Add ${PROJECT_NAME} coverage reports" OFF)

#set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
#include(compiler-flags)

add_subdirectory(src)

if (${PROJECT_NAME}_BUILD_TESTING)
enable_testing()
add_subdirectory(test)
endif()
28 changes: 28 additions & 0 deletions lattice/cpp/templates/project-src-cmake.txt.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
file(GLOB lib_headers "${PROJECT_SOURCE_DIR}/include/{{project_name}}/*.h")
file(GLOB lib_src "${PROJECT_SOURCE_DIR}/src/*.cpp")

set (sources "${lib_headers}"
"${lib_src}")

option(${PROJECT_NAME}_STATIC_LIB "Make ${PROJECT_NAME} a static library" ON)

if (${PROJECT_NAME}_STATIC_LIB)
add_library(${PROJECT_NAME} STATIC ${sources})
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-D${PROJECT_NAME}_STATIC_DEFINE")
else ()
set(CMAKE_MACOSX_RPATH 1)
add_library(${PROJECT_NAME} SHARED ${sources})
endif ()

#target_link_libraries({{project_name}} PUBLIC courierr fmt nlohmann_json)
target_include_directories({{project_name}} PUBLIC ${PROJECT_SOURCE_DIR}/include/{{project_name}})

target_compile_options(${PROJECT_NAME} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4>
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wextra -Wpedantic>
)

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME})
2 changes: 1 addition & 1 deletion lattice/file_io.py
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ def dump(content, output_file_path):
elif ext in [".yaml", ".yml"]:
with open(output_file_path, "w", encoding="utf-8") as out_file:
yaml.dump(content, out_file, sort_keys=False)
elif ext in [".h", ".cpp"]:
elif ext in [".h", ".cpp", ".txt"]:
with open(output_file_path, "w", encoding="utf-8") as src:
src.write(content)
src.write("\n")
11 changes: 8 additions & 3 deletions lattice/lattice.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
from .docs import MkDocsWeb, DocumentFile
from .header_entries import HeaderTranslator
from .cpp_entries import CPPTranslator
from lattice.cpp.generate_support_headers import generate_support_headers, support_header_pathnames
from lattice.cpp.generate_support_headers import generate_support_headers, support_header_pathnames, generate_build_support


class SchemaFile: # pylint:disable=R0902
@@ -328,9 +328,12 @@ def setup_cpp_source_files(self):
"""Create directories for generated CPP source"""
self.cpp_output_dir = Path(self.build_directory) / "cpp"
make_dir(self.cpp_output_dir)
include_dir = make_dir(self.cpp_output_dir / "include")
include_dir = make_dir(include_dir / f"{self.root_directory.name}")
src_dir = make_dir(self.cpp_output_dir / "src")
for schema in self.cpp_schemas:
schema.cpp_header_path = self.cpp_output_dir / f"{schema.file_base_name.lower()}.h"
schema.cpp_source_path = self.cpp_output_dir / f"{schema.file_base_name.lower()}.cpp"
schema.cpp_header_path = include_dir / f"{schema.file_base_name.lower()}.h"
schema.cpp_source_path = src_dir / f"{schema.file_base_name.lower()}.cpp"

def cpp_support_headers(self) -> list[Path]:
return support_header_pathnames(self.cpp_output_dir)
@@ -348,3 +351,5 @@ def generate_cpp_headers(self):
c.translate(self.root_directory.name, h)
dump(str(c), schema.cpp_source_path)
generate_support_headers(self.root_directory.name, root_groups, self.cpp_output_dir)
generate_build_support(self.root_directory.name, self.cpp_output_dir)

0 comments on commit 5ee1b65

Please sign in to comment.