Makefile

Description

This is an example makefile that you can use for your WebC projects.

The makefile takes a list of source files. If the extension ends in .wc or one of the supported file types then the makefile will run the WebC processor to build a .c file in the build directory. The makefile will then build the .c to a .o and link the program.

Binary files are sent into WebC to be converted to .c's and then compiled as normal.

makefile

CC = gcc
CC_FLAGS = -fno-builtin-printf -std=gnu99 -g --short-enums -Wall -fmax-errors=1 -Wfatal-errors
LNK_FLAGS = 

WebCC = webcc
WebCC_FLAGS = 

# Final binary
BIN = Example

# Put all auto generated stuff to this build dir.
BUILD_DIR = ./build

SOURCE_DIR = src

WCExtList=wc png pdf css js apng bmp gif svg webp wav avi bin doc docx gz jar json jsonld \
	mp3 mpeg odp ods odt oga ogv otf ppt pptx rar rtf swf tar ttf txt vsd woff woff2 xls \
	xlsx zip 7z tiff tif ico cur jpg jpeg jfif pjpeg pjp

# List of all the source files.
SOURCE = main.c \
	BittyHTTP/SocketsCon.c \
	BittyHTTP/WebServer.c \
	ExampleWebC.wc \
	ExamplePNG.png \

INCLUDES = src

.PHONY: PreBuild

# All .o files go to build dir.
OBJ = $(SOURCE:%.c=$(BUILD_DIR)/%.o)
$(foreach ext,$(WCExtList),$(eval OBJ := $(OBJ:%.$(ext)=$(BUILD_DIR)/%_$(ext).o)))

WCC = $(filter-out %.c,$(SOURCE))
$(foreach ext,$(WCExtList),$(eval WCC := $(WCC:%.$(ext)=$(BUILD_DIR)/%_$(ext).c)))

# Gcc/Clang will create these .d files containing dependencies.
DEP = $(OBJ:%.o=%.d)
# Include paths with a -I in front of them
CC_INCLUDE = $(INCLUDES:%= -I %)

# Default target named after the binary.
$(BIN) : $(BUILD_DIR)/$(BIN)

# Actual target of the binary - depends on all .o files.
$(BUILD_DIR)/$(BIN) : $(WCC) $(OBJ)
	echo Linking...
	# Create build directories - same structure as sources.
	mkdir -p $(@D)
	# Just link all the object files.
	$(CC) $(CC_FLAGS) $(OBJ) $(LNK_FLAGS) -o $(BIN)

# Include all .d files
-include $(DEP)

# Build target for every single object file.
# The potential dependency on header files is covered
# by calling `-include $(DEP)`.
$(BUILD_DIR)/%.o : $(SOURCE_DIR)/%.c
	echo Compiling $(notdir $<)
	mkdir -p $(@D)
	# The -MMD flags additionaly creates a .d file with
	# the same name as the .o file.
	$(CC) $(CC_FLAGS) $(CC_INCLUDE) -MMD -c $< -o $@

$(BUILD_DIR)/%.o : $(BUILD_DIR)/%.c
	echo Compiling $(notdir $<)
	$(CC) $(CC_FLAGS) $(CC_INCLUDE) -MMD -c $< -o $@

define WebCCompile
$(patsubst %.$(1),$(BUILD_DIR)/%_$(1).c,$(filter %.$(1),$(SOURCE))): $(BUILD_DIR)/%_$(1).c: $(SOURCE_DIR)/%.$(1)
	echo WebCompiling "$$(notdir $$<)"
	mkdir -p $$(@D)
	$$(WebCC) $$(WebCC_FLAGS) $$< -o $$@
endef

$(foreach ext,$(WCExtList),$(eval $(call WebCCompile,$(ext))))

#.PHONY : clean
clean :
	# This should remove all generated files.
	-rm $(BIN) $(OBJ) $(DEP) $(WCC)

Simple Hello World

Files

src/HelloWorld.wc

make

make

echo WebCompiling "HelloWorld.wc"
WebCompiling HelloWorld.wc
mkdir -p build
webcc  src/HelloWorld.wc -o build/HelloWorld.c
echo Compiling HelloWorld.c
Compiling HelloWorld.c
gcc -fno-builtin-printf -std=gnu99 -g --short-enums -Wall -fmax-errors=1 -Wfatal-errors  -I src -MMD -c build/HelloWorld.c -o build/HelloWorld.o
echo Linking...
Linking...
# Create build directories - same structure as sources.
mkdir -p build
# Just link all the object files.
gcc -fno-builtin-printf -std=gnu99 -g --short-enums -Wall -fmax-errors=1 -Wfatal-errors ./build/HelloWorld.o ./build/BittyHTTP/SocketsCon.o ./build/BittyHTTP/WebServer.o ./build/FileServer.o -o HellowWorld.exe

Building Graphics

Files

src/main.c
src/graphics/Picture.png

make

make

echo WebCompiling "Picture.png"
WebCompiling Picture.png
mkdir -p build/graphics
webcc  src/graphics/Picture.png -o build/graphics/Picture.c
echo Compiling main.c
Compiling main.c
gcc -fno-builtin-printf -std=gnu99 -g --short-enums -Wall -fmax-errors=1 -Wfatal-errors  -I src -MMD -c build/main.c -o build/main.o
echo Compiling Picture.c
Compiling Picture.c
gcc -fno-builtin-printf -std=gnu99 -g --short-enums -Wall -fmax-errors=1 -Wfatal-errors  -I src -MMD -c build/graphics/Picture.c -o build/graphics/Picture.o
echo Linking...
Linking...
# Create build directories - same structure as sources.
mkdir -p build
# Just link all the object files.
gcc -fno-builtin-printf -std=gnu99 -g --short-enums -Wall -fmax-errors=1 -Wfatal-errors ./build/main.o ./build/BittyHTTP/SocketsCon.o ./build/BittyHTTP/WebServer.o ./build/FileServer.o -o Graphics.exe