Skip to content

Commit 4d53389

Browse files
authored
Merge pull request #111 from macuser47/cmake-port
[WIP] Add basic CMake implementation
2 parents 0aca334 + a8fd99f commit 4d53389

File tree

11 files changed

+306
-15
lines changed

11 files changed

+306
-15
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v1
1212
- name: test
13-
run: make && make examples
13+
run: mkdir build && cd build && cmake .. && make

CMAKE_README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# How to use CMake
2+
3+
This document gives an overview of the basics of cmake and how to use
4+
it to make libraries and executables for `pod-embedded`.
5+
6+
## Cmake basics
7+
8+
CMake projects are specified using a `CMakeFiles.txt` in each
9+
library/exectutable directory.
10+
11+
### Basic CMakeFiles.txt layout
12+
A top level CMakeFiles.txt always contains a `cmake_minimum_required`
13+
and a `project` declaration. The rest is populated with `add_subdirectory`,
14+
calls, which include CMakeFiles.txts from subdirectories.
15+
16+
### Variables
17+
CMake varaibles are specified using `set(VARIABLE_NAME value...)`, which
18+
sets the variable `VARIABLE_NAME` to any subsequent values (multiple values
19+
will make a list.)
20+
21+
Eg:
22+
23+
```
24+
#set importantDirectory to /usr/local/bin
25+
set(importantDirectory /usr/local/bin)
26+
27+
#set libraries to list containing middleware, drivers, utils
28+
set(libraries middleware
29+
drivers
30+
utils)
31+
```
32+
33+
The fundametnal primitives when defining a cmake project are
34+
executables and libraries.
35+
36+
## Making a library
37+
38+
To create a library that is imported by other executables, we use the following
39+
40+
```
41+
#Define the project for the library
42+
project(foobarlib VERSION 1.0
43+
DESCRIPTION "The foobar library"
44+
LANGUAGES CXX)
45+
46+
#Define the library target called foobar with all of its source directories
47+
#Files specified with respect to CMakeLists.txt for the library
48+
add_library(foobar src/somefile.c
49+
src/bin.c
50+
src/baz.c)
51+
52+
#Add any header files by specifying the include directory, in this case
53+
#called include
54+
target_include_directories(foobar PUBLIC include)
55+
56+
#add any library dependencies
57+
target_link_libraries(foobar PUBLIC someotherlibrary verynicedependency)
58+
target_link_libraries(foobar PRIVATE thisonelibrary)
59+
60+
#export library to cmake file so it can be imported elsewhere
61+
export(TARGETS foobar FILE FoobarConfig.cmake)
62+
```
63+
64+
In this example we define a project called `foobarlib` in its own CMakeLists.txt
65+
and define a library called `foobar` with its respective source files.
66+
Then any header files are added by specifying an include directory with `target_include_directories(...)`. Then we link any other libraries `foobar` depends on with `target_link_libraries(...)`. Finally we export the library using `export(...)` so the library can be imported elsewhere in the project.
67+
68+
### PUBLIC vs PRIVATE vs INTERFACE
69+
Whenever a dependency is specified for a project, you must specify if that dependency
70+
is `PUBLIC`, `PRIVATE` or `INTERFACE`. This changes the availability of the dependencies
71+
you're adding to any project that depends on the library you're writing.
72+
73+
For example, if library `foo` depends on library `bar` publically, then
74+
any library that depends on `foo` will be linked with headers from `bar`.
75+
76+
The rule of thumb is:
77+
78+
* If your source files depend on the library, make it `PRIVATE`.
79+
* If your header files depend on the library, make it `INTERFACE`
80+
* If both of the above are true, make it `PUBLIC`
81+
82+
Dependencies for executables should always be `PRIVATE`.
83+
84+
## Making an executable
85+
86+
To make an executable, simply use
87+
88+
```
89+
add_executable(binaryName, sourceFile1.c
90+
sourceFile2.c
91+
sourceFileN.c)
92+
```
93+
94+
and specify includes and dependencies with `target_include_directories(...)` and `target_link_libraries(...)` in the same way as with libraries.
95+

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
project(PodEmbedded VERSION 1.0
4+
DESCRIPTION "Pod embedded")
5+
6+
#put binaries in out/
7+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out/lib)
8+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out/lib)
9+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out)
10+
11+
#add cmake projects from subdirectories
12+
add_subdirectory(embedded/app)
13+
add_subdirectory(embedded/data)
14+
add_subdirectory(embedded/drivers)
15+
add_subdirectory(embedded/examples)
16+
add_subdirectory(embedded/peripherals)
17+
add_subdirectory(embedded/utils)
18+
add_subdirectory(middleware)

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,38 @@
22

33
[![Actions Status](https://github.com/badgerloop-software/pod-embedded/workflows/CI/badge.svg)](https://github.com/badgerloop-software/pod-embedded/actions)
44

5-
*Developers: Rohan Daruwala, Ezra Boley*
5+
*Developers: Rohan Daruwala, Ezra Boley, Nic Hodlofski*
66

77
The embedded repository for Badgerloop's pod in the Hyperloop Competition
88

99
## Beaglebone Make Instructions
1010

11-
There are currently 3 sets of targets:
11+
To build all compile targets:
1212

13-
1) Making the main programs (`badgerloop_LV` and `badgerloop_HV`), placed in the `out/` folder
13+
1) Make a directory called build in the root of the repository and enter it
1414

1515
```
16-
make
16+
mkdir build
17+
cd build
1718
```
1819

19-
The main target can also be executed in a special debug mode, adding useful
20-
print outs from various modules. Any of the following targets can also take
21-
advantage of the added debug functionality. To build in debug mode, run:
20+
2) Run cmake on the parent directory
2221

2322
```
24-
make DEBUG=1
23+
cmake ..
2524
```
2625

27-
2) Making the examples, placed into the `out/tests` folder
26+
3) Build the project with make
2827

2928
```
30-
make examples
29+
make
3130
```
3231

33-
3) Making utilities for showcasing various parts of the pod's functionality
32+
There are currently 3 sets of targets:
3433

35-
```
36-
make utils
37-
```
34+
The main programs (`badgerloop_LV` and `badgerloop_HV`) are placed in the `out/` directory
35+
36+
Tests will be placed in `out/tests`, and utilities in `out/utils`.
3837

3938
### Adding Tests
4039

embedded/app/CMakeLists.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#setup library for the stuff in src/ because it's used elsewhere >_>
2+
project(applib VERSION 1.0
3+
DESCRIPTION "main"
4+
LANGUAGES CXX)
5+
6+
#yeah, it's annoying, but glob has problems with needing to run cmake again,
7+
#and it's SUPER discouraged.
8+
set(dependecies src/bms_fault_checking.c
9+
src/nav.c
10+
src/state_machine.c
11+
src/init.c
12+
src/pressure_fault_checking.c
13+
src/states.c
14+
src/motor.c
15+
src/rms_fault_checking.c
16+
src/transitions.c)
17+
18+
#required libraries for the main pod executables
19+
set(libraries middleware
20+
data
21+
peripherals
22+
drivers
23+
Threads::Threads) #so pthreads work
24+
25+
#configure app library
26+
add_library(app ${dependecies})
27+
target_include_directories(app PUBLIC include)
28+
target_link_libraries(app PRIVATE ${libraries})
29+
30+
#export app library so it can be used externally
31+
export(TARGETS app FILE AppLibConfig.cmake)
32+
33+
#configure HV and LV executables
34+
find_package(Threads REQUIRED)
35+
add_executable(badgerloop_HV main/badgerloop_HV.cpp ${dependecies})
36+
add_executable(badgerloop_LV main/badgerloop_LV.cpp ${dependecies})
37+
38+
target_link_libraries(badgerloop_HV PRIVATE ${libraries})
39+
target_link_libraries(badgerloop_LV PRIVATE ${libraries})
40+
41+
target_include_directories(badgerloop_HV PRIVATE include)
42+
target_include_directories(badgerloop_LV PRIVATE include)

embedded/data/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project(datalib VERSION 1.0
2+
DESCRIPTION "data"
3+
LANGUAGES CXX)
4+
5+
add_library(data src/filters.c)
6+
7+
target_include_directories(data PUBLIC include)
8+
9+
export(TARGETS data FILE DataConfig.cmake)

embedded/drivers/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
project(driverslib VERSION 1.0
2+
DESCRIPTION "Drivers for the pod"
3+
LANGUAGES C)
4+
5+
add_library(drivers src/bbgpio.c
6+
src/can.c
7+
src/i2c.c)
8+
9+
target_include_directories(drivers PUBLIC include)
10+
11+
export(TARGETS drivers FILE DriversConfig.cmake)

embedded/examples/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#Define example executables
2+
add_executable(bmsDisplay bmsDisplay.cpp)
3+
add_executable(dashTest dashTest.cpp)
4+
add_executable(navTest navTest.cpp)
5+
add_executable(solenoidTest solenoidTest.c)
6+
add_executable(brakingTest brakingTest.cpp)
7+
add_executable(gpioHvTest gpioHvTest.c)
8+
add_executable(oldMotorTest oldMotorTest.c)
9+
add_executable(retroTest retroTest.c)
10+
add_executable(stateTest stateTest.c)
11+
add_executable(can_test can_test.c)
12+
add_executable(imu_test imu_test.c)
13+
add_executable(presTest presTest.c)
14+
15+
#Link library dependencies for each example
16+
target_link_libraries(imu_test PRIVATE peripherals data)
17+
target_link_libraries(can_test PRIVATE drivers peripherals data)
18+
target_link_libraries(stateTest PRIVATE peripherals data app)
19+
target_link_libraries(retroTest PRIVATE drivers peripherals data)
20+
target_link_libraries(oldMotorTest PRIVATE drivers middleware peripherals data)
21+
target_link_libraries(presTest PRIVATE middleware peripherals data)
22+
target_link_libraries(dashTest PRIVATE middleware peripherals data app)
23+
target_link_libraries(navTest PRIVATE middleware peripherals data app)
24+
target_link_libraries(brakingTest PRIVATE drivers middleware peripherals data)
25+
target_link_libraries(bmsDisplay PRIVATE middleware peripherals data)
26+
target_link_libraries(gpioHvTest PRIVATE drivers peripherals data)
27+
target_link_libraries(solenoidTest PRIVATE drivers peripherals data)
28+
29+
#Make sure each executable generates in out/tests
30+
set_target_properties(bmsDisplay
31+
dashTest
32+
navTest
33+
solenoidTest
34+
brakingTest
35+
gpioHvTest
36+
oldMotorTest
37+
retroTest
38+
stateTest
39+
can_test
40+
imu_test
41+
presTest
42+
PROPERTIES
43+
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/out/tests)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
project(peripheralslib VERSION 1.0
2+
DESCRIPTION "Peripherals"
3+
LANGUAGES CXX)
4+
#Define sources for library
5+
add_library(peripherals src/batt.c
6+
src/braking.c
7+
src/hv_iox.c
8+
src/lv_iox.c
9+
src/lv_iox.c
10+
src/NCD9830DBR2G.c
11+
src/retro.c
12+
src/bms.c
13+
src/can_devices.c
14+
src/imu.c
15+
src/mcp23017.c
16+
src/proc_iox.c
17+
src/rms.c)
18+
19+
target_include_directories(peripherals PUBLIC include)
20+
21+
target_link_libraries(peripherals PRIVATE data app)
22+
target_link_libraries(peripherals PUBLIC drivers)
23+
24+
export(TARGETS peripherals FILE PeripheralsConfig.cmake)

embedded/utils/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Define exectutables
2+
add_executable(gpioUtil gpioUtil.c)
3+
add_executable(rmsProg rmsProg.c)
4+
5+
#Link library dependencies
6+
target_link_libraries(gpioUtil PRIVATE drivers peripherals)
7+
target_link_libraries(rmsProg PRIVATE drivers peripherals data)
8+
9+
#set output directory to out/utils
10+
set_target_properties(gpioUtil rmsProg
11+
PROPERTIES
12+
RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/out/utils")

0 commit comments

Comments
 (0)