-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUILD][MISC] Rewritten all Makefiles for efficiency and correctness #269
Changes from all commits
cb53587
700355b
987c734
2345b21
bd12817
487b4cf
89a35b7
e0117fd
c628ca4
486715d
ce0e068
11db2bb
c82d403
4c8ebec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# This Makefile defines a bunch of stuff used by the lower-level Makefiles | ||
# all file paths are from their perspective! | ||
|
||
CC = gcc | ||
|
||
BIN = ../bin | ||
BUILD = ../build | ||
OBJ = $(BUILD)/obj | ||
|
||
# this is the name of the lower level directory (e.g. THIS_DIR in the dev_handler folder is "dev_handler") | ||
THIS_DIR = $(strip $(shell pwd | xargs basename -z)) | ||
|
||
# list of runtime component directories | ||
COMPONENT_DIRS = dev_handler net_handler executor network_switch shm_wrapper runtime_util logger | ||
# list of subfolders in $(OBJ) for runtime object files | ||
OBJ_SUBDIR = $(foreach dir,$(COMPONENT_DIRS),$(OBJ)/$(dir)) | ||
# list of folders to include in compilation commands | ||
INCLUDE += $(foreach dir,$(COMPONENT_DIRS),-I../$(dir)) | ||
|
||
# list of all object files this executable is dependent on relative to this Makefile | ||
OBJS = $(patsubst %.c,$(OBJ)/$(THIS_DIR)/%.o,$(SRCS)) | ||
|
||
# list of build folders | ||
BUILD_DIR += $(OBJ) $(BIN) $(BUILD) $(OBJ_SUBDIR) | ||
|
||
# append -MMD and -MP to CFLAGS to get the dependency files built for the object files | ||
CFLAGS += -MMD -MP | ||
|
||
# general rule for compiling a list of source files to object files in the $(OBJ) directory | ||
$(OBJ)/$(THIS_DIR)/%.o: %.c | $(OBJ_SUBDIR) | ||
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ | ||
|
||
# general rule for making a build directory | ||
$(BUILD_DIR): | ||
mkdir -p $@ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
LIBFLAGS=-pthread -lrt -Wall | ||
BIN=../bin | ||
# list of libraries that dev_handler needs to compile | ||
LIBS=-pthread -lrt -Wall | ||
|
||
dev_handler: dev_handler.c message.c message.h ../logger/logger.c ../runtime_util/runtime_util.c ../shm_wrapper/shm_wrapper.c | ||
gcc $^ -o $(BIN)/dev_handler $(LIBFLAGS) | ||
# list of source files that the target (dev_handler) depends on, relative to this folder | ||
SRCS = dev_handler.c dev_handler_message.c ../logger/logger.c ../runtime_util/runtime_util.c ../shm_wrapper/shm_wrapper.c | ||
|
||
# specify the target (executable we want to make) | ||
TARGET = dev_handler | ||
|
||
.PHONY: all clean $(TARGET) | ||
|
||
all: $(TARGET) | ||
|
||
# include top-level Makefile for some common variables and rules | ||
include ../Makefile_defs | ||
|
||
# resolve phony target "dev_handler" as ../bin/dev_handler | ||
$(TARGET): $(BIN)/$(TARGET) | ||
|
||
# rule to compile dev_handler | ||
$(BIN)/$(TARGET): $(OBJS) | $(BIN) | ||
$(CC) $(OBJS) -o $@ $(LIBS) | ||
|
||
# remove build artifacts | ||
clean: | ||
rm -f $(BIN)/dev_handler | ||
rm -f dev_handler | ||
|
||
rm -f $(OBJS) | ||
rm -f $(patsubst %.o,%.d,$(OBJS)) | ||
rm -f $(BIN)/$(TARGET) | ||
|
||
-include $(patsubst %.o,%.d,$(OBJS)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little complex. So basically the Here are a couple of good articles explaining it: https://nathandumont.com/blog/automatically-detect-changes-in-header-files-in-a |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,45 @@ | ||
# Change this to point to your specific Python version. Need >= 3.6 | ||
py=python3.10 | ||
# list of libraries that executor needs to compile | ||
LIBS=-pthread -lrt -Wall -export-dynamic -fPIC | ||
|
||
CFLAGS=$(shell $(py)-config --cflags) | ||
LDFLAGS=$(shell $(py)-config --ldflags) | ||
PY_LIB=-l$(py) | ||
LIBS=-pthread -lrt -export-dynamic -fPIC -lprotobuf-c | ||
#-rdynamic or -export-dynamic to make it dynamically linked | ||
BIN=../bin | ||
# list of source files that the target (executor) depends on, relative to this folder | ||
SRCS = executor.c gamestate_filter.c ../logger/logger.c ../runtime_util/runtime_util.c ../shm_wrapper/shm_wrapper.c | ||
|
||
all: executor studentapi.c | ||
# Python compilation definitions | ||
PY_VER = python3.10 | ||
PY_LIB = -l$(PY_VER) | ||
CFLAGS = $(shell $(PY_VER)-config --cflags) | ||
|
||
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 | ||
gcc $(CFLAGS) $^ -o $(BIN)/$@ $(LIBS) $(PY_LIB) | ||
# specify the target (executable we want to make) | ||
TARGET = executor | ||
|
||
studentapi.c: studentapi.pyx runtime.pxd | ||
$(py) setup.py build_ext -i | ||
.PHONY: all clean $(TARGET) | ||
|
||
all: $(TARGET) studentapi.c | ||
|
||
# include top-level Makefile for common variables and rules | ||
include ../Makefile_defs | ||
|
||
$(TARGET): $(BIN)/$(TARGET) | ||
|
||
# rule to compile executor | ||
$(BIN)/$(TARGET): $(OBJS) | $(BIN) | ||
$(CC) $(OBJS) -o $@ $(LIBS) $(PY_LIB) | ||
|
||
# rule to compile studentapi.c | ||
studentapi.c: studentapi.pyx studentapi.pxd setup.py | ||
$(PY_VER) setup.py build_ext -i | ||
|
||
# rule to compile testapi | ||
test_api: studentapi.c test_studentapi.py | ||
- $(py) test_studentapi.py | ||
- $(PY_VER) test_studentapi.py | ||
|
||
# remove build artifacts | ||
clean: | ||
rm -f studentapi.c | ||
rm -f $(BIN)/executor | ||
rm -f executor | ||
rm -f $(OBJS) | ||
rm -f $(patsubst %.o,%.d,$(OBJS)) | ||
rm -rf studentapi.c | ||
rm -f *.so | ||
rm -rf build | ||
rm -f $(BIN)/$(TARGET) | ||
|
||
-include $(patsubst %.o,%.d,$(OBJS)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ben: "basically it tells make that the thing listed after .PHONY is not a real file; in this case we don’t actually make a file called all, clean, or dev_handler (we make a file called ../bin/dev_handler)
basically it prevents make from redo-ing commands, like say we want to have make dev_handler build ../bin/dev_handler. if we don’t declare dev_handler as .PHONY then when you run make dev_handler it will always try to look for a file called dev_handler, which will never exist (cuz really the file is ../bin/dev_handler ) so it will run the compilation command again when it doesn’t need to"
Adding it here for future reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clean up this message a bit since that was something I hastily wrote in Slack:
If you're completely confused, read our wiki page on
make
here.But in short, this Makefile contains the targets
all
,clean
, anddev_handler
. However, none of these targets are real files (there is no file atruntime/dev_handler/all
,runtime/dev_handler/clean
, orruntime/dev_handler/dev_handler
). That last one might be a bit confusing--thedev_handler
executable is located inruntime/bin/dev_handler
, so from the perspective of the directory that this Makefile is in, the path is../bin/dev_handler
.Thus, if we just left the Makefile with these three targets, each time we ran
make all
ormake clean
ormake dev_handler
, it would try to find a file calledall
,clean
, ordev_handler
, respectively, fail to find the file (since we never make a file with that name), and always run the recipe (which is not what we want to do!). We only want to run the recipe if there is something to be made.Thus, we tell
make
that these three special targets are not real files by labeling them as.PHONY
. Thus, when we domake dev_handler
, it will it won't automatically build everything just because it can't find thedev_handler
file (which will never exist).