Skip to content

Commit

Permalink
cleaner / consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Mar 30, 2024
1 parent 241cf4b commit a1bed76
Show file tree
Hide file tree
Showing 40 changed files with 243 additions and 48 deletions.
Binary file added PHENICS_i2i_cmake_survival_2024.ppt
Binary file not shown.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ sudo apt-get install cmake cmake-curses-gui g++ ninja-build make ccache
Random number reference: https://xkcd.com/221


# Homework

Create a CMake project that generates a python3 module using nanobind in C++

https://github.com/wjakob/nanobind

https://nanobind.readthedocs.io/en/latest/building.html

The cmake should generate a python module with a name similar to

`my_module.cpython-311-x86_64-linux-gnu.so`

As an example if you're using python3.11

So the following python3 code should work: https://gist.github.com/PhilipDeegan/08984d7243e69e7dd1e244f78a6f0423
With the following C++ module file: https://gist.github.com/PhilipDeegan/775f4acd668ef46d046714c3d4863287


# misc
Expand Down
4 changes: 4 additions & 0 deletions tut/000/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@

## Create simple executable

Exercise:

Comment out the first line (cmake_minimum_required) and execute `./run.sh`
2 changes: 1 addition & 1 deletion tut/001/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ project(hello_world)

add_executable(${PROJECT_NAME} main.cpp)
enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})
14 changes: 14 additions & 0 deletions tut/001/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@

## Create simple executable and configure it as a test via ctest

Exercise:

Add to run.sh

```
export CXX=ccg
```

or to cmake command

```
-DCMAKE_CXX_COMPILER=ccg
```
8 changes: 7 additions & 1 deletion tut/001/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
set -e
CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" && cd $CWD

rm -rf build && mkdir build && cd build && cmake .. && make
# export CXX=ccg

rm -rf build
mkdir build
cd build
cmake .. # -DCMAKE_CXX_COMPILER=ccg
make
ctest -V
7 changes: 2 additions & 5 deletions tut/002/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cmake_minimum_required (VERSION 3.18)

project(hello_library)

set(PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Expand All @@ -9,9 +8,7 @@ include_directories(${PROJECT_DIR}/inc) # global headers
add_subdirectory(src)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} project_library)
target_link_libraries(${PROJECT_NAME} PRIVATE project_library)

enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})


add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})
15 changes: 15 additions & 0 deletions tut/002/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@

## Create static library and link it to executable

Scope keywords

PRIVATE - Only used for target
INTERFACE - Only used for targets that consume this target
PUBLIC - Both

https://cmake.org/cmake/help/latest/command/target_link_libraries.html#command:target_link_libraries


Exercise: try add in `src/CMakeLists.txt`

```
target_compile_definitions(${PROJECT_NAME} PRIVATE ABC=1 INTERFACE XYZ=2)
```
5 changes: 2 additions & 3 deletions tut/002/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" && cd $CWD

rm -rf build
cmake -B build
cd build
cmake --build .
ctest -V
cmake --build build -v
cmake --build build -t test
9 changes: 4 additions & 5 deletions tut/002/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
project(project_library)

cmake_minimum_required (VERSION 3.18)

set(SOURCE_CPP src.cpp)

add_library(${PROJECT_NAME} STATIC ${SOURCE_CPP} ${SOURCE_INC})
project(project_library)

add_library(${PROJECT_NAME} STATIC src.cpp)
# target_compile_definitions(${PROJECT_NAME} PRIVATE ABC=1 INTERFACE XYZ=2)
4 changes: 2 additions & 2 deletions tut/003/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ include_directories(${PROJECT_DIR}/inc) # global headers
add_subdirectory(src)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} project_library)
target_link_libraries(${PROJECT_NAME} PRIVATE project_library)

enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})


8 changes: 3 additions & 5 deletions tut/003/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
project(project_library)

cmake_minimum_required (VERSION 3.18)

set(SOURCE_CPP src.cpp)

add_library(${PROJECT_NAME} SHARED ${SOURCE_CPP} ${SOURCE_INC})
project(project_library)

add_library(${PROJECT_NAME} SHARED src.cpp)
4 changes: 2 additions & 2 deletions tut/004/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ include_directories(${PROJECT_DIR}/inc) # global headers
add_subdirectory(src)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} project_library)
target_link_libraries(${PROJECT_NAME} PRIVATE project_library)

enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})
6 changes: 5 additions & 1 deletion tut/004/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" && cd $CWD
time (
date

rm -rf build && mkdir build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug && make VERBOSE=1
rm -rf build
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug
make VERBOSE=1
ctest -V

) 1> >(tee $CWD/.run.sh.out ) 2> >(tee $CWD/.run.sh.err >&2 )
5 changes: 3 additions & 2 deletions tut/004/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required (VERSION 3.18)

project(project_library)

set(SOURCE_CPP src.cpp)

add_library(${PROJECT_NAME} STATIC ${SOURCE_CPP} ${SOURCE_INC})
add_library(${PROJECT_NAME} STATIC src.cpp)

4 changes: 2 additions & 2 deletions tut/005/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ include_directories(${PROJECT_DIR}/inc) # global headers
add_subdirectory(src)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} project_library)
target_link_libraries(${PROJECT_NAME} PRIVATE project_library)

enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})


6 changes: 5 additions & 1 deletion tut/005/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" && cd $CWD
time (
date

rm -rf build && mkdir build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && make VERBOSE=1
rm -rf build
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make VERBOSE=1
ctest -V

) 1> >(tee $CWD/.run.sh.out ) 2> >(tee $CWD/.run.sh.err >&2 )
5 changes: 3 additions & 2 deletions tut/005/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required (VERSION 3.18)

project(project_library)


set(SOURCE_CPP src.cpp)

add_library(${PROJECT_NAME} STATIC ${SOURCE_CPP} ${SOURCE_INC})
add_library(${PROJECT_NAME} STATIC src.cpp)

4 changes: 2 additions & 2 deletions tut/006/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ include_directories(${PROJECT_DIR}/inc) # global headers
add_subdirectory(src)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} project_library)
target_link_libraries(${PROJECT_NAME} PRIVATE project_library)

enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})


3 changes: 3 additions & 0 deletions tut/006/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
## Create release static library and link it to executable with Ninja

Ninja is typically faster than Make

Generators:
https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html
5 changes: 3 additions & 2 deletions tut/006/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required (VERSION 3.18)

project(project_library)


set(SOURCE_CPP src.cpp)

add_library(${PROJECT_NAME} STATIC ${SOURCE_CPP} ${SOURCE_INC})
add_library(${PROJECT_NAME} STATIC src.cpp)

4 changes: 2 additions & 2 deletions tut/007/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ include_directories(${PROJECT_DIR}/inc) # global headers
add_subdirectory(src)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} project_library)
target_link_libraries(${PROJECT_NAME} PRIVATE project_library)

enable_testing()
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})


6 changes: 3 additions & 3 deletions tut/007/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(project_library)
cmake_minimum_required (VERSION 3.18)

project(project_library)

set(SOURCE_CPP src.cpp)

add_library(${PROJECT_NAME} STATIC ${SOURCE_CPP} ${SOURCE_INC})
add_library(${PROJECT_NAME} STATIC src.cpp)

2 changes: 1 addition & 1 deletion tut/008/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ endif(BUILD_RELEASE)

enable_testing()
add_executable(${PROJECT_NAME} main.cpp)
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})


4 changes: 2 additions & 2 deletions tut/009/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

enable_testing()
add_executable(${PROJECT_NAME} main.cpp)
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME ${PROJECT_NAME} COMMAND ./${PROJECT_NAME})

include(FetchContent)

Expand All @@ -25,4 +25,4 @@ FetchContent_Declare(
GIT_REPOSITORY https://github.com/LaboratoryOfPlasmaPhysics/cppdict
)
FetchContent_MakeAvailable(cppdict)
target_link_libraries(${PROJECT_NAME} cppdict)
target_link_libraries(${PROJECT_NAME} PRIVATE cppdict)
2 changes: 1 addition & 1 deletion tut_adv/001/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.18)
cmake_minimum_required (VERSION 3.19)

project(tracer CXX)
add_executable(${PROJECT_NAME} main.cpp)
Expand Down
24 changes: 24 additions & 0 deletions tut_adv/001/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 19,
"patch": 0
},
"configurePresets": [
{
"name": "default",
"description": "Base preset for library developers",
"binaryDir": "${sourceDir}/build"
},
{
"name": "dev-werror",
"description": "Linux preset for library developers",
"hidden": false,
"inherits": "default",
"cacheVariables": {
"CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Wshadow -Wconversion -Wsign-conversion -Wcast-align -Wcast-qual -Wnull-dereference -Woverloaded-virtual -Wformat=2 -Werror"
}
}
]
}
6 changes: 5 additions & 1 deletion tut_adv/001/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
set -e
CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" && cd $CWD

rm -rf build && mkdir build && cd build && cmake .. && make VERBOSE=1
rm -rf build
mkdir build
cd build
cmake .. # --preset dev-werror
make VERBOSE=1
gdb -batch -ex run -ex bt --args ./tracer || true # non-zero exit code expected
2 changes: 0 additions & 2 deletions tut_adv/002/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ cmake_minimum_required (VERSION 3.18)
project(optimizations CXX)
add_executable(${PROJECT_NAME} main.cpp)

# set(CMAKE_BUILD_TYPE "Debug")

add_executable(optimize_O0 main.cpp)
target_compile_options(optimize_O0 PRIVATE -O0 -g3 -fopt-info-vec)
target_link_options(optimize_O0 PRIVATE -O0 -g3 )
Expand Down
1 change: 1 addition & 0 deletions tut_adv/003/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.run.sh.*
10 changes: 10 additions & 0 deletions tut_adv/003/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required (VERSION 3.18)
project(linkage)

SET(CMAKE_SKIP_RPATH FALSE)

add_subdirectory(lib1)
add_subdirectory(lib2)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE lib2)
6 changes: 6 additions & 0 deletions tut_adv/003/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


# Multi library link chain visibility

Here we will demonstrate how private shared linking works.
And how it can minimize project configuration complexity
5 changes: 5 additions & 0 deletions tut_adv/003/lib1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project(lib1)


add_library(${PROJECT_NAME} SHARED src/src.cpp inc/lib1.hpp)
target_include_directories( ${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc)
10 changes: 10 additions & 0 deletions tut_adv/003/lib1/inc/lib1.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _LIB1_NS_INC_HPP_
#define _LIB1_NS_INC_HPP_

namespace lib1_ns{

void func();

}

#endif /*_LIB1_NS_INC_HPP_*/
11 changes: 11 additions & 0 deletions tut_adv/003/lib1/src/src.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#include "lib1.hpp"

#include <iostream>

namespace lib1_ns{

void func(){ std::cout << __FILE__ << " " << __LINE__ << std::endl; }

}

7 changes: 7 additions & 0 deletions tut_adv/003/lib2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project(lib2)


add_library(${PROJECT_NAME} SHARED src/src.cpp inc/lib2.hpp)
target_include_directories( ${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc)

target_link_libraries(${PROJECT_NAME} PRIVATE lib1)
Loading

0 comments on commit a1bed76

Please sign in to comment.