Factor common userland and baremetal C functions

This allows add.c to run unmodified on both!

For that to work, use int main on baremetal, and pass the return value to
the final exit.
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2018-11-15 00:00:00 +00:00
parent ecc2a21b57
commit 26b890f42f
26 changed files with 87 additions and 52 deletions

View File

@@ -1,13 +1,17 @@
.PHONY: all clean mkdir
CCFLAGS = -ggdb3 -I$(COMMON_DIR) -O0 -Wall -Werror -Wextra -Wno-unused-function $(CCFLAGS_EXTRA) $(CCFLAGS_SCRIPT)
CFLAGS = -fopenmp -std=c99 $(CCFLAGS) $(CFLAGS_EXTRA)
CXXFLAGS = -std=c++17 $(CCFLAGS) $(CXXFLAGS_EXTRA)
# -Wno-unused-function for function definitions on headers,
# because we are lazy to make a shared object. TODO.
CCFLAGS = -ggdb3 -O0 -Wall -Werror -Wextra -Wno-unused-function $(CCFLAGS_EXTRA)
COMMON_DIR = ..
COMMON_BASENAME = common
COMMON_OBJ = $(OUT_DIR)/$(COMMON_BASENAME)$(OBJ_EXT)
IN_EXT_C = .c
IN_EXT_CXX = .cpp
LIBS = -lm
OBJ_EXT = .o
OUT_EXT = .out
OUT_DIR = .
@@ -38,14 +42,17 @@ OUTS := $(addprefix $(OUT_DIR)/,$(OUTS))
all: mkdir $(OUTS)
$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_C)
$(CC) $(CFLAGS) -o '$@' '$<' $(LIBS)
$(COMMON_OBJ): $(COMMON_DIR)/$(COMMON_BASENAME)$(IN_EXT_C)
$(CC) $(CFLAGS) -c -o '$@' '$<' $(LIBS)
$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_CXX)
$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_C) $(COMMON_OBJ)
$(CC) $(CFLAGS) $(COMMON_OBJ) -o '$@' '$<' $(LIBS)
$(OUT_DIR)/%$(OUT_EXT): %$(IN_EXT_CXX) $(COMMON_OBJ)
$(CXX) $(CXXFLAGS) -o '$@' '$<' $(LIBS)
clean:
rm -f *'$(OUT_EXT)'
rm -f *'$(OBJ_EXT)' *'$(OUT_EXT)'
mkdir:
mkdir -p '$(OUT_DIR)'

1
userland/add.c Symbolic link
View File

@@ -0,0 +1 @@
../baremetal/add.c

View File

@@ -10,7 +10,7 @@
#include <sys/mman.h>
#include <unistd.h> /* sysconf */
#include "common.h" /* virt_to_phys_user */
#include "common_userland.h" /* virt_to_phys_user */
enum { BUFFER_SIZE = 4 };

View File

@@ -1,7 +1,7 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#blas
* Adapted from: https://github.com/xianyi/OpenBLAS/wiki/User-Manual/59b62f98e7400270fb03ad1d85fba5b64ebbff2b#call-cblas-interface */
#include "common.h"
#include "common_userland.h"
#include <assert.h>
#include <cblas.h>

View File

@@ -9,7 +9,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "common.h" /* pagemap_get_entry */
#include "common_userland.h" /* pagemap_get_entry */
int main(int argc, char **argv)
{

View File

@@ -4,7 +4,7 @@
#include <stdio.h> /* printf */
#include <stdlib.h> /* EXIT_SUCCESS, EXIT_FAILURE, strtoull */
#include "common.h" /* virt_to_phys_user */
#include "common_userland.h" /* virt_to_phys_user */
int main(int argc, char **argv)
{