Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a C++ test outline #2

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
29 changes: 28 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ concurrency:
cancel-in-progress: true

jobs:

tests:
runs-on: ubuntu-latest
steps:
Expand All @@ -18,3 +17,31 @@ jobs:
with:
python-version: '3.12'
- run: pip install kaitaistruct

test-cpp:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

# Need for a recent version of cmake
- name: Install kitware cmake archive
run: |
curl -LO https://apt.kitware.com/kitware-archive.sh
sudo bash kitware-archive.sh

- name: Install compiler and cmake
run: |
sudo apt update -y
sudo apt install -y build-essential cmake

# The version here should match the one in ci/cpp/CMakeLists.txt
- name: Install kaitai-struct-compiler
run: |
curl -LO https://github.com/kaitai-io/kaitai_struct_compiler/releases/download/0.10/kaitai-struct-compiler_0.10_all.deb
sudo apt install -y ./kaitai-struct-compiler_0.10_all.deb

- name: Build C++ test executable
run: |
cd ci/cpp
cmake -S . -B build
cmake --build build
69 changes: 69 additions & 0 deletions ci/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
cmake_minimum_required(VERSION 3.26)
include(FetchContent)
project(test-cpp CXX)

# Version of the kaitai_struct_cpp_stl_runtime
# Should match that of the kaitai-struct-compiler
set(KSCSR_VERSION "0.10")

set(kaitai_struct_cpp_stl_runtime_BUILD_TESTS OFF CACHE INTERNAL
"Disable tests in katai_struct_cpp_stl build" FORCE)

FetchContent_Declare(
kaitai_struct_cpp_stl_runtime
GIT_REPOSITORY https://github.com/kaitai-io/kaitai_struct_cpp_stl_runtime.git
GIT_TAG ${KSCSR_VERSION}
GIT_SHALLOW ON
FIND_PACKAGE_ARGS
)

FetchContent_MakeAvailable(kaitai_struct_cpp_stl_runtime)

# Check kaitai-struct-compiler version
execute_process(
COMMAND bash -c "kaitai-struct-compiler --version | awk '{ print $2 }'"
OUTPUT_VARIABLE KSC_VERSION
)

if(NOT ${KSC_VERSION} STREQUAL ${KSCSR_VERSION})
message(FATAL "kaitai-struct-compiler version ${KSC_VERSION} does not match kaitai_struct_cpp_stl_runtime-struct version ${KSCSR_VERSION}")
endif()

# Convenient link to the directory containing the ksy files
set(TEMPLATE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../src/tablestream/templates)

# Generate the cpp files from the various ksy files
add_custom_command(OUTPUT dtype.cpp metadata.cpp VERBATIM
DEPENDS ${TEMPLATE_DIR}/metadata.ksy
COMMAND kaitai-struct-compiler -t cpp_stl --cpp-standard=11
${TEMPLATE_DIR}/metadata.ksy
)

add_custom_command(OUTPUT lock.cpp VERBATIM
DEPENDS ${TEMPLATE_DIR}/lock.ksy
COMMAND kaitai-struct-compiler -t cpp_stl --cpp-standard=11
${TEMPLATE_DIR}/lock.ksy
)

add_custom_command(OUTPUT standard_storage_manager.cpp VERBATIM
DEPENDS ${TEMPLATE_DIR}/standard_storage_manager.ksy
COMMAND kaitai-struct-compiler -t cpp_stl --cpp-standard=11
${TEMPLATE_DIR}/standard_storage_manager.ksy
)

add_executable(test-cpp
dtype.cpp
lock.cpp
metadata.cpp
standard_storage_manager.cpp
test.cpp)

target_include_directories(test-cpp
PRIVATE ${kaitai_struct_cpp_stl_runtime_SOURCE_DIR})
target_link_libraries(test-cpp
PRIVATE kaitai_struct_cpp_stl_runtime)
# Allows compilation to go further, but these should be fixed
target_compile_options(test-cpp PRIVATE
$<$<CXX_COMPILER_ID:GNU>:-fpermissive>
$<$<CXX_COMPILER_ID:Clang>:-fpermissive>
)
8 changes: 8 additions & 0 deletions ci/cpp/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# C++ test case

Basic test case testing that generated C++ code compiles and links.

.. code-block:: bash

$ cmake -S . -B build
$ cmake --build build
5 changes: 5 additions & 0 deletions ci/cpp/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <kaitai/kaitaistruct.h>

int main(int argc, char * argv[]) {
return 0;
}
Loading