Skip to content

Commit d10e210

Browse files
authored
[BUILD][MISC] Merge pull request #269 from pioneers/makefile_tests_new
[BUILD][MISC] Rewritten all Makefiles for efficiency and correctness
2 parents 9d16be8 + 4c8ebec commit d10e210

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+589
-838
lines changed

.clang-format-ignore

-3
This file was deleted.

.clang-tidy

-51
This file was deleted.

.github/workflows/pull_request_ci.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ jobs:
8989
tar -xzf protobuf-c-1.4.1.tar.gz
9090
cd protobuf-c-1.4.1 && ./configure && make && sudo make install && sudo ldconfig
9191
92+
# Before we build Runtime, we check the formatting
93+
- name: Format
94+
run: |
95+
./runtime format check
96+
9297
# Now that we are done installing Runtime's dependencies, we build Runtime
9398
- name: Build
9499
run: |
@@ -97,4 +102,4 @@ jobs:
97102
# And finally, we test Runtime
98103
- name: Test
99104
run: |
100-
./runtime test
105+
./runtime test

.gitignore

+2-23
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
bin/*
33
tests/bin/*
44

5-
# ignore the tests/devices
6-
tests/devices
7-
85
# ignore the entire lowcar/Device folder
96
lowcar/Device
107

@@ -24,7 +21,7 @@ network_switch/exit_status.txt
2421
*.pyc
2522
*.pyo
2623
__pycache__/
27-
build/
24+
executor/build/
2825

2926
# VS Code
3027
.vscode
@@ -38,29 +35,11 @@ build/
3835
*.o
3936
*.obj
4037

41-
# Precompiled Headers
42-
*.gch
43-
*.pch
44-
4538
# Compiled Dynamic libraries
4639
*.so
4740
*.dylib
4841
*.dll
4942
*.pyd
5043

51-
# Fortran module files
52-
*.mod
53-
*.smod
54-
55-
# Compiled Static libraries
56-
*.lai
57-
*.la
58-
*.a
59-
*.lib
60-
61-
# Executables
62-
*.exe
63-
*.out
64-
*.app
65-
44+
# .DS_Store
6645
*.DS_Store

Makefile_defs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This Makefile defines a bunch of stuff used by the lower-level Makefiles
2+
# all file paths are from their perspective!
3+
4+
CC = gcc
5+
6+
BIN = ../bin
7+
BUILD = ../build
8+
OBJ = $(BUILD)/obj
9+
10+
# this is the name of the lower level directory (e.g. THIS_DIR in the dev_handler folder is "dev_handler")
11+
THIS_DIR = $(strip $(shell pwd | xargs basename -z))
12+
13+
# list of runtime component directories
14+
COMPONENT_DIRS = dev_handler net_handler executor network_switch shm_wrapper runtime_util logger
15+
# list of subfolders in $(OBJ) for runtime object files
16+
OBJ_SUBDIR = $(foreach dir,$(COMPONENT_DIRS),$(OBJ)/$(dir))
17+
# list of folders to include in compilation commands
18+
INCLUDE += $(foreach dir,$(COMPONENT_DIRS),-I../$(dir))
19+
20+
# list of all object files this executable is dependent on relative to this Makefile
21+
OBJS = $(patsubst %.c,$(OBJ)/$(THIS_DIR)/%.o,$(SRCS))
22+
23+
# list of build folders
24+
BUILD_DIR += $(OBJ) $(BIN) $(BUILD) $(OBJ_SUBDIR)
25+
26+
# append -MMD and -MP to CFLAGS to get the dependency files built for the object files
27+
CFLAGS += -MMD -MP
28+
29+
# general rule for compiling a list of source files to object files in the $(OBJ) directory
30+
$(OBJ)/$(THIS_DIR)/%.o: %.c | $(OBJ_SUBDIR)
31+
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
32+
33+
# general rule for making a build directory
34+
$(BUILD_DIR):
35+
mkdir -p $@

dev_handler/Makefile

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1-
LIBFLAGS=-pthread -lrt -Wall
2-
BIN=../bin
1+
# list of libraries that dev_handler needs to compile
2+
LIBS=-pthread -lrt -Wall
33

4-
dev_handler: dev_handler.c message.c message.h ../logger/logger.c ../runtime_util/runtime_util.c ../shm_wrapper/shm_wrapper.c
5-
gcc $^ -o $(BIN)/dev_handler $(LIBFLAGS)
4+
# list of source files that the target (dev_handler) depends on, relative to this folder
5+
SRCS = dev_handler.c dev_handler_message.c ../logger/logger.c ../runtime_util/runtime_util.c ../shm_wrapper/shm_wrapper.c
66

7+
# specify the target (executable we want to make)
8+
TARGET = dev_handler
9+
10+
.PHONY: all clean $(TARGET)
11+
12+
all: $(TARGET)
13+
14+
# include top-level Makefile for some common variables and rules
15+
include ../Makefile_defs
16+
17+
# resolve phony target "dev_handler" as ../bin/dev_handler
18+
$(TARGET): $(BIN)/$(TARGET)
19+
20+
# rule to compile dev_handler
21+
$(BIN)/$(TARGET): $(OBJS) | $(BIN)
22+
$(CC) $(OBJS) -o $@ $(LIBS)
23+
24+
# remove build artifacts
725
clean:
8-
rm -f $(BIN)/dev_handler
9-
rm -f dev_handler
10-
26+
rm -f $(OBJS)
27+
rm -f $(patsubst %.o,%.d,$(OBJS))
28+
rm -f $(BIN)/$(TARGET)
29+
30+
-include $(patsubst %.o,%.d,$(OBJS))

dev_handler/dev_handler.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
#include <termios.h> // for POSIX terminal control definitions in serialport_open()
77

8-
#include "../logger/logger.h"
9-
#include "../runtime_util/runtime_util.h"
10-
#include "../shm_wrapper/shm_wrapper.h"
11-
#include "message.h"
8+
#include <dev_handler_message.h>
9+
#include <logger.h>
10+
#include <runtime_util.h>
11+
#include <shm_wrapper.h>
1212

1313
/**
1414
* Each device will have a unique port number.
@@ -24,7 +24,7 @@
2424
* These file paths may also be referred to as "port_prefix" in the code.
2525
*/
2626
#define LOWCAR_FILE_PATH "/dev/ttyACM"
27-
#define VIRTUAL_FILE_PATH "ttyACM" // will be created in the home directory
27+
#define VIRTUAL_FILE_PATH "ttyACM" // will be created in the home directory
2828
#define LOWCAR_USB_FILE_PATH "/dev/ttyUSB"
2929

3030
// **************************** PRIVATE STRUCT ****************************** //
@@ -93,7 +93,7 @@ uint32_t used_lowcar_usb_ports = 0;
9393
pthread_mutex_t used_ports_lock; // poll_connected_devices() and relay_clean_up() shouldn't access used_ports at the same time
9494

9595
// String to hold the home directory path (for looking for virtual device sockets)
96-
const char *home_dir;
96+
const char* home_dir;
9797

9898
#define MAX_PORT_NAME_SIZE 64
9999

@@ -395,7 +395,7 @@ void relay_clean_up(relay_t* relay) {
395395
pthread_mutex_unlock(&used_ports_lock);
396396
pthread_mutex_destroy(&relay->relay_lock);
397397
pthread_cond_destroy(&relay->start_cond);
398-
if (relay->dev_id.uid == -1) {
398+
if (relay->dev_id.uid == (uint64_t) -1) {
399399
char port_name[MAX_PORT_NAME_SIZE];
400400
construct_port_name(port_name, relay->is_virtual, relay->is_usb, relay->port_num);
401401
log_printf(DEBUG, "Cleaned up bad device %s\n", port_name);
@@ -582,7 +582,7 @@ int receive_message(relay_t* relay, message_t* msg) {
582582
char port_name[MAX_PORT_NAME_SIZE];
583583
construct_port_name(port_name, relay->is_virtual, relay->is_usb, relay->port_num);
584584

585-
if (relay->dev_id.uid == -1) {
585+
if (relay->dev_id.uid == (uint64_t) -1) {
586586
/* Haven't verified device is lowcar yet
587587
* read() is set to timeout while waiting for an ACK (see serialport_open())*/
588588
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
@@ -872,7 +872,7 @@ int main(int argc, char* argv[]) {
872872
// If SIGINT (Ctrl+C) is received, call stop() to clean up
873873
signal(SIGINT, stop);
874874
init();
875-
home_dir = getenv("HOME"); // set the home directory
875+
home_dir = getenv("HOME"); // set the home directory
876876
log_printf(INFO, "DEV_HANDLER initialized.");
877877
poll_connected_devices();
878878
return 0;

dev_handler/message.c dev_handler/dev_handler_message.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include "message.h"
1+
#include <dev_handler_message.h>
22

33
// ******************************** Utility ********************************* //
44

55
void print_bytes(uint8_t* data, size_t len) {
66
printf("0x");
7-
for (int i = 0; i < len; i++) {
7+
for (size_t i = 0; i < len; i++) {
88
printf("%02X ", data[i]);
99
}
1010
printf("\n");
@@ -71,7 +71,7 @@ static int append_payload(message_t* msg, uint8_t* data, size_t len) {
7171
*/
7272
static uint8_t checksum(uint8_t* data, size_t len) {
7373
uint8_t chk = data[0];
74-
for (int i = 1; i < len; i++) {
74+
for (size_t i = 1; i < len; i++) {
7575
chk ^= data[i];
7676
}
7777
return chk;
@@ -292,7 +292,7 @@ ssize_t message_to_bytes(message_t* msg, uint8_t cobs_encoded[], size_t len) {
292292
}
293293
data[0] = msg->message_id;
294294
data[1] = msg->payload_length;
295-
for (int i = 0; i < msg->payload_length; i++) {
295+
for (size_t i = 0; i < msg->payload_length; i++) {
296296
data[i + MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE] = msg->payload[i];
297297
}
298298
data[MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + msg->payload_length] = checksum(data, MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + msg->payload_length);
@@ -317,7 +317,7 @@ int parse_message(uint8_t data[], message_t* msg_to_fill) {
317317
// Smaller than valid message
318318
free(decoded);
319319
return 3;
320-
} else if (ret > (MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + MAX_PAYLOAD_SIZE + CHECKSUM_SIZE)) {
320+
} else if (ret > (int) (MESSAGE_ID_SIZE + PAYLOAD_LENGTH_SIZE + MAX_PAYLOAD_SIZE + CHECKSUM_SIZE)) {
321321
// Larger than the largest valid message
322322
free(decoded);
323323
return 3;

dev_handler/message.h dev_handler/dev_handler_message.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#ifndef MESSAGE_H
1111
#define MESSAGE_H
1212

13-
#include "../logger/logger.h"
14-
#include "../runtime_util/runtime_util.h"
13+
#include <logger.h>
14+
#include <runtime_util.h>
1515

1616
/* The maximum number of milliseconds to wait between each DEVICE_PING from a device
1717
* Waiting for this long will exit all threads for that device (doing cleanup as necessary)

executor/Makefile

+36-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
1-
# Change this to point to your specific Python version. Need >= 3.6
2-
py=python3.10
1+
# list of libraries that executor needs to compile
2+
LIBS=-pthread -lrt -Wall -export-dynamic -fPIC
33

4-
CFLAGS=$(shell $(py)-config --cflags)
5-
LDFLAGS=$(shell $(py)-config --ldflags)
6-
PY_LIB=-l$(py)
7-
LIBS=-pthread -lrt -export-dynamic -fPIC -lprotobuf-c
8-
#-rdynamic or -export-dynamic to make it dynamically linked
9-
BIN=../bin
4+
# list of source files that the target (executor) depends on, relative to this folder
5+
SRCS = executor.c gamestate_filter.c ../logger/logger.c ../runtime_util/runtime_util.c ../shm_wrapper/shm_wrapper.c
106

11-
all: executor studentapi.c
7+
# Python compilation definitions
8+
PY_VER = python3.10
9+
PY_LIB = -l$(PY_VER)
10+
CFLAGS = $(shell $(PY_VER)-config --cflags)
1211

13-
executor: executor.c gamestate_filter.c ../shm_wrapper/shm_wrapper.c ../logger/logger.c ../runtime_util/runtime_util.c ../net_handler/net_util.c ../net_handler/pbc_gen/text.pb-c.c
14-
gcc $(CFLAGS) $^ -o $(BIN)/$@ $(LIBS) $(PY_LIB)
12+
# specify the target (executable we want to make)
13+
TARGET = executor
1514

16-
studentapi.c: studentapi.pyx runtime.pxd
17-
$(py) setup.py build_ext -i
15+
.PHONY: all clean $(TARGET)
1816

17+
all: $(TARGET) studentapi.c
18+
19+
# include top-level Makefile for common variables and rules
20+
include ../Makefile_defs
21+
22+
$(TARGET): $(BIN)/$(TARGET)
23+
24+
# rule to compile executor
25+
$(BIN)/$(TARGET): $(OBJS) | $(BIN)
26+
$(CC) $(OBJS) -o $@ $(LIBS) $(PY_LIB)
27+
28+
# rule to compile studentapi.c
29+
studentapi.c: studentapi.pyx studentapi.pxd setup.py
30+
$(PY_VER) setup.py build_ext -i
31+
32+
# rule to compile testapi
1933
test_api: studentapi.c test_studentapi.py
20-
- $(py) test_studentapi.py
34+
- $(PY_VER) test_studentapi.py
2135

36+
# remove build artifacts
2237
clean:
23-
rm -f studentapi.c
24-
rm -f $(BIN)/executor
25-
rm -f executor
38+
rm -f $(OBJS)
39+
rm -f $(patsubst %.o,%.d,$(OBJS))
40+
rm -rf studentapi.c
2641
rm -f *.so
42+
rm -rf build
43+
rm -f $(BIN)/$(TARGET)
44+
45+
-include $(patsubst %.o,%.d,$(OBJS))

0 commit comments

Comments
 (0)