From 2b9cd2d15254810adc42afc439fa861cb342a5c5 Mon Sep 17 00:00:00 2001 From: fusiled Date: Fri, 5 Jun 2026 15:40:35 -0500 Subject: [PATCH] Add example to docs --- .github/workflows/verify.yml | 2 + .gitignore | 1 + CMakeLists.txt | 56 ++++++-- README.md | 9 +- docs/README.md | 2 +- .../1_FirstModule_and_SimoSim/CMakeLists.txt | 41 ++++++ .../1_FirstModule_and_SimoSim/FirstModule.cc | 121 ++++++++++++++++++ .../1_FirstModule_and_SimoSim/README.md | 76 +++++++++++ .../1_FirstModule_and_SimoSim/flake.lock | 82 ++++++++++++ .../1_FirstModule_and_SimoSim/flake.nix | 34 +++++ .../system_config.yaml | 31 +++++ docs/tutorial/README.md | 3 - flake.lock | 8 +- flake.nix | 4 +- include/Simo/core/Log.h | 8 +- include/Simo/core/Time.h | 4 +- include/Simo/module/Module.h | 11 +- sgconfig.yml | 6 + src/SimoSim/SimoSim.cc | 11 ++ src/core/Context.cc | 2 +- src/core/Log.cc | 6 +- src/module/Module.cc | 6 + support/ast-grep/.gitkeep | 0 support/ast-grep/no-default-arguments.yaml | 15 +++ support/cmake/SimoConfig.cmake | 4 + support/scripts/run-ast-grep.sh | 30 +++++ tests/Simo/SimulationContextTest.cc | 35 +++++ tests/collection/PingPongCollection.cc | 56 +++----- tests/module/ModuleTest.cc | 14 +- 29 files changed, 605 insertions(+), 73 deletions(-) create mode 100644 docs/guided_examples/1_FirstModule_and_SimoSim/CMakeLists.txt create mode 100644 docs/guided_examples/1_FirstModule_and_SimoSim/FirstModule.cc create mode 100644 docs/guided_examples/1_FirstModule_and_SimoSim/README.md create mode 100644 docs/guided_examples/1_FirstModule_and_SimoSim/flake.lock create mode 100644 docs/guided_examples/1_FirstModule_and_SimoSim/flake.nix create mode 100644 docs/guided_examples/1_FirstModule_and_SimoSim/system_config.yaml delete mode 100644 docs/tutorial/README.md create mode 100644 sgconfig.yml create mode 100644 support/ast-grep/.gitkeep create mode 100644 support/ast-grep/no-default-arguments.yaml create mode 100644 support/cmake/SimoConfig.cmake create mode 100755 support/scripts/run-ast-grep.sh diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 43633e4..1cc88f3 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -30,6 +30,8 @@ jobs: github_access_token: ${{ secrets.GITHUB_TOKEN }} - name: clang-format run: nix develop --command ${{github.workspace}}/support/scripts/cpp-check-format.sh + - name: ast-grep + run: nix develop --command ${{github.workspace}}/support/scripts/run-ast-grep.sh Configure-Build-and-Unit-Tests: strategy: matrix: diff --git a/.gitignore b/.gitignore index e198963..faf5f38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.idea /cmake-build-* +/docs/tutorial/build* diff --git a/CMakeLists.txt b/CMakeLists.txt index e15b34d..5c828aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,14 +102,51 @@ target_include_directories(Simo ) target_include_directories(Simo PRIVATE src) +# Simo_includes_only is a header-only target +add_library(Simo_includes_only INTERFACE) +target_include_directories(Simo_includes_only + INTERFACE + $ + $ +) +target_link_libraries(Simo_includes_only INTERFACE glaze::glaze) +target_compile_options(Simo_includes_only INTERFACE -fno-rtti) +# Avoid linking against Simo +if (APPLE) + target_link_options(Simo_includes_only INTERFACE + "LINKER:-undefined,dynamic_lookup" + ) +else() + target_link_options(Simo_includes_only INTERFACE + "LINKER:--allow-shlib-undefined" + ) +endif() + add_executable(SimoSim src/SimoSim/SimoSim.cc) target_link_libraries(SimoSim PRIVATE Simo) -install(TARGETS Simo SimoSim) +install(TARGETS Simo SimoSim Simo_includes_only + EXPORT SimoTargets +) install(DIRECTORY include/ DESTINATION include ) +install(EXPORT SimoTargets + FILE SimoTargets.cmake + NAMESPACE Simo:: + DESTINATION lib/cmake/Simo +) + +# Generate the Package Configuration +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + "cmake/Simo/SimoConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion) +install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/support/cmake/SimoConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/cmake/Simo/SimoConfigVersion.cmake" + DESTINATION lib/cmake/Simo) + ## Unit tests @@ -171,19 +208,14 @@ target_link_libraries(test_Module PRIVATE Simo) add_library(SimoTestPingPongCollection SHARED tests/collection/PingPongCollection.cc ) +set_target_properties(SimoTestPingPongCollection PROPERTIES + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN 1 +) target_compile_options(SimoTestPingPongCollection PRIVATE -fno-exceptions) # Compile only with includes. Do not link against libraries -target_link_libraries(SimoTestPingPongCollection PRIVATE $) -# Avoid linking against Simo -if (APPLE) - target_link_options(SimoTestPingPongCollection PRIVATE - "LINKER:-undefined,dynamic_lookup" - ) -else () - target_link_options(SimoTestPingPongCollection PRIVATE - "LINKER:--allow-shlib-undefined" - ) -endif () +target_link_libraries(SimoTestPingPongCollection PRIVATE Simo_includes_only) + # Make this custom command to trigger re-build for targets that uses SimoTestPingPongCollection libraries indirectly (like # test_Collection) diff --git a/README.md b/README.md index 711c819..51590fb 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,6 @@ Guides (Markdown files inside `docs` folder) and Doxygen built from the main bra ```bash git clone https://github.com/fusiled/Simo cd Simo -git submodule update --init ``` ## Build @@ -41,6 +40,10 @@ The [nix](https://nixos.org/learn/) flake simplifies the setup of the dependenci - Run `nix develop` to enter a virtual environment with all the required tools installed. The GitHub actions run entering the virtual environment of `nix develop`. +## First Steps + +Look inside [docs/tutorial](./docs/tutorial) . + ## Features - Loading components from shared objects at runtime ([examples](./tests/collection/CollectionTest.cc)) @@ -65,7 +68,5 @@ This can be achieved with: ## To Do - Create a collector with periodic window collection functionality -- Create logging system for components -- More documentation and examples -- Error config in Simo::Context +- More documentation and examples - Benchmark Simo against Gem5 and SystemC diff --git a/docs/README.md b/docs/README.md index 45f3c72..abac076 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,4 @@ -Find the tutorial in [tutorial folder](./tutorial) +Find the tutorial in [guided_examples folder](./guided_examples) ## Motivations diff --git a/docs/guided_examples/1_FirstModule_and_SimoSim/CMakeLists.txt b/docs/guided_examples/1_FirstModule_and_SimoSim/CMakeLists.txt new file mode 100644 index 0000000..cedd6b9 --- /dev/null +++ b/docs/guided_examples/1_FirstModule_and_SimoSim/CMakeLists.txt @@ -0,0 +1,41 @@ +# Copyright 2026 Matteo Fusi and Contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cmake_minimum_required(VERSION 3.31.0) +project(SimoTutorial VERSION 0.0.1) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + + +# Look for Simo package +find_package(Simo REQUIRED) +get_filename_component(SIMO_PACKAGE_PREFIX "${Simo_DIR}/../../.." ABSOLUTE) + + +# Create you collection of modules +add_library(TutorialCollection SHARED + FirstModule.cc +) +# Hide symbols by default +set_target_properties(TutorialCollection PROPERTIES + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN 1 +) + +# Use Simo::Simo_includes_only to only use the include files and avoid direct linking to Simo +target_link_libraries(TutorialCollection PRIVATE Simo::Simo_includes_only) +target_include_directories(TutorialCollection PRIVATE + "${SIMO_PACKAGE_PREFIX}/include" +) diff --git a/docs/guided_examples/1_FirstModule_and_SimoSim/FirstModule.cc b/docs/guided_examples/1_FirstModule_and_SimoSim/FirstModule.cc new file mode 100644 index 0000000..a685923 --- /dev/null +++ b/docs/guided_examples/1_FirstModule_and_SimoSim/FirstModule.cc @@ -0,0 +1,121 @@ +/* + * Copyright 2026 Matteo Fusi and Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Note 0: this is the general include for Simo +#include + +namespace Simo::Examples { + +/// Log periodically a message and count the number of time the log took place +class SIMO_PUBLIC FirstModule : public Module { + public: + // Note 1 + // Module initialization takes place here + InitializationStatus initialize(Context& sim_ctx_p, + const Parameters& parameters) override { + // Call initialization of subclass + if (const auto status = Module::initialize(sim_ctx_p, parameters); + !status.success()) { + // Fail if something bad happened + return status; + } + // Initialize variables from parameters + period = parameters.get