Skip to content

Commit

Permalink
cleaner / consistency (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Mar 31, 2024
1 parent 241cf4b commit f04977c
Show file tree
Hide file tree
Showing 41 changed files with 251 additions and 61 deletions.
Binary file added PHENICS_i2i_cmake_survival_2024.ppt
Binary file not shown.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@ then click -> "Open Editor" for VScode type editor

### Debian / Apt Setup

```bash
sudo apt-get install cmake cmake-curses-gui g++ ninja-build make ccache

```

### Appendix:

Random number reference: https://xkcd.com/221




# misc

# modern cmake
https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1
10 changes: 10 additions & 0 deletions tut/000/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@

## Create simple executable

# Exercise:

Comment out the first line of `CMakeLists.txt` (cmake_minimum_required) and execute `./run.sh`
A comment is all text after a hash symbol, eg:

```
# this line is comment
# cmake_minimum_required (VERSION 3.0) # this does not execute now
```
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
10 changes: 4 additions & 6 deletions tut_adv/002/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ 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 )
target_link_options(optimize_O0 PRIVATE -O0 -g3 )

add_executable(optimize_O1 main.cpp)
target_compile_options(optimize_O1 PRIVATE -O1 -fopt-info-vec)
Expand All @@ -20,10 +18,10 @@ target_link_options(optimize_O2 PRIVATE -O2)

# probably not safe redistributable
add_executable(optimize_O3 main.cpp)
target_compile_options(optimize_O3 PRIVATE -O3 -fopt-info-vec)
target_compile_options(optimize_O3 PRIVATE -O3 -fopt-info-vec)
target_link_options(optimize_O3 PRIVATE -O3)

# very likely not safe redistributable, but best local optimizations
add_executable(optimize_O3_native main.cpp)
target_compile_options(optimize_O3_native PRIVATE -O3 -march=native -mtune=native -fopt-info-vec)
target_link_options(optimize_O3_native PRIVATE -O0 -march=native -mtune=native)
target_compile_options(optimize_O3_native PRIVATE -O3 -march=native -mtune=native -fopt-info-vec)
target_link_options(optimize_O3_native PRIVATE -O3 -march=native -mtune=native)
14 changes: 12 additions & 2 deletions tut_adv/002/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

Example of automatic vectorization

# Exercise - deduplicate lines 6 to 27 with a function

```
function(add_my_exe target src cxx_flags link_flags)
# fill this in
endfunction(add_my_exe)
```

## Appendix:

Expand All @@ -27,7 +34,7 @@ Command reference: https://stackoverflow.com/a/2224357/795574

if so, the output might look like

```
```cxx
#define __AVX__ 1
#define __AVX2__ 1
```
Expand All @@ -39,6 +46,9 @@ https://en.wikipedia.org/wiki/SSE2
try execute `diff_defines.py` to see what's enabled via `-march=native`
```python3
```bash
python3 tut_adv/002/diff_defines.py
```

Function solution:
https://gist.github.com/PhilipDeegan/c3d098f8fded6978856b290b9ed60590
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.*
Loading

0 comments on commit f04977c

Please sign in to comment.