forked from ground/ground
Allow static linking to Ground
This commit is contained in:
9
Makefile
9
Makefile
@@ -1,4 +1,5 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
|
AR = ar
|
||||||
CFLAGS += -Wall -Wextra -Isrc/include -Iinclude -ggdb
|
CFLAGS += -Wall -Wextra -Isrc/include -Iinclude -ggdb
|
||||||
LDFLAGS += -ldl -rdynamic
|
LDFLAGS += -ldl -rdynamic
|
||||||
|
|
||||||
@@ -17,6 +18,7 @@ OBJ_DIR = $(BUILD_DIR)/obj
|
|||||||
# Output names
|
# Output names
|
||||||
EXECUTABLE = $(BIN_DIR)/ground
|
EXECUTABLE = $(BIN_DIR)/ground
|
||||||
SHARED_LIB = $(LIB_DIR)/libgroundvm.so
|
SHARED_LIB = $(LIB_DIR)/libgroundvm.so
|
||||||
|
STATIC_LIB = $(LIB_DIR)/libgroundvm.a
|
||||||
HEADERS = $(INC_DIR)/groundvm.h $(INC_DIR)/groundext.h
|
HEADERS = $(INC_DIR)/groundvm.h $(INC_DIR)/groundext.h
|
||||||
|
|
||||||
# Source files
|
# Source files
|
||||||
@@ -41,7 +43,7 @@ executable: $(EXECUTABLE)
|
|||||||
|
|
||||||
# Build shared library
|
# Build shared library
|
||||||
.PHONY: library
|
.PHONY: library
|
||||||
library: $(SHARED_LIB) $(HEADERS)
|
library: $(SHARED_LIB) $(STATIC_LIB) $(HEADERS)
|
||||||
|
|
||||||
# Build both
|
# Build both
|
||||||
.PHONY: both
|
.PHONY: both
|
||||||
@@ -59,6 +61,10 @@ $(EXECUTABLE): $(EXE_OBJECTS) | $(BIN_DIR)
|
|||||||
$(SHARED_LIB): $(LIB_OBJECTS) | $(LIB_DIR)
|
$(SHARED_LIB): $(LIB_OBJECTS) | $(LIB_DIR)
|
||||||
$(CC) -shared $(LIB_OBJECTS) -o $@ $(LDFLAGS)
|
$(CC) -shared $(LIB_OBJECTS) -o $@ $(LDFLAGS)
|
||||||
|
|
||||||
|
# Build static library
|
||||||
|
$(STATIC_LIB): $(LIB_OBJECTS) | $(LIB_DIR)
|
||||||
|
$(AR) rcs $(STATIC_LIB) $(LIB_OBJECTS)
|
||||||
|
|
||||||
# Copy header for library distribution
|
# Copy header for library distribution
|
||||||
$(INC_DIR)/%.h: include/%.h | $(INC_DIR)
|
$(INC_DIR)/%.h: include/%.h | $(INC_DIR)
|
||||||
cp $< $@
|
cp $< $@
|
||||||
@@ -88,6 +94,7 @@ install: both
|
|||||||
mkdir -p $(DESTDIR)$(PREFIX)/include
|
mkdir -p $(DESTDIR)$(PREFIX)/include
|
||||||
cp $(EXECUTABLE) $(DESTDIR)$(PREFIX)/bin/
|
cp $(EXECUTABLE) $(DESTDIR)$(PREFIX)/bin/
|
||||||
cp $(SHARED_LIB) $(DESTDIR)$(PREFIX)/lib/
|
cp $(SHARED_LIB) $(DESTDIR)$(PREFIX)/lib/
|
||||||
|
cp $(STATIC_LIB) $(DESTDIR)$(PREFIX)/lib/
|
||||||
cp $(HEADERS) $(DESTDIR)$(PREFIX)/include/
|
cp $(HEADERS) $(DESTDIR)$(PREFIX)/include/
|
||||||
echo "$(DESTDIR)$(PREFIX)/lib" | tee $(DESTDIR)/etc/ld.so.conf.d/cground.conf
|
echo "$(DESTDIR)$(PREFIX)/lib" | tee $(DESTDIR)/etc/ld.so.conf.d/cground.conf
|
||||||
ldconfig
|
ldconfig
|
||||||
|
|||||||
@@ -5,6 +5,42 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
char* getFileContents(const char* filename) {
|
||||||
|
// https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c
|
||||||
|
FILE* fp;
|
||||||
|
long lSize;
|
||||||
|
char* file;
|
||||||
|
|
||||||
|
fp = fopen(filename, "rb");
|
||||||
|
if (!fp) {
|
||||||
|
perror(filename);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(fp, 0L, SEEK_END);
|
||||||
|
lSize = ftell(fp);
|
||||||
|
rewind(fp);
|
||||||
|
|
||||||
|
file = calloc(1, lSize + 1);
|
||||||
|
if (!file) {
|
||||||
|
fclose(fp);
|
||||||
|
fputs("memory allocation fail when reading file", stderr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1!=fread(file, lSize, 1, fp)) {
|
||||||
|
fclose(fp);
|
||||||
|
free(file);
|
||||||
|
fputs("couldn't read entire file", stderr);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// we done
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
GroundProgram groundCreateProgram() {
|
GroundProgram groundCreateProgram() {
|
||||||
return (GroundProgram) {
|
return (GroundProgram) {
|
||||||
|
|||||||
36
src/main.c
36
src/main.c
@@ -7,41 +7,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
char* getFileContents(const char* filename) {
|
char* getFileContents(const char* filename);
|
||||||
// https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c
|
|
||||||
FILE* fp;
|
|
||||||
long lSize;
|
|
||||||
char* file;
|
|
||||||
|
|
||||||
fp = fopen(filename, "rb");
|
|
||||||
if (!fp) {
|
|
||||||
perror(filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
fseek(fp, 0L, SEEK_END);
|
|
||||||
lSize = ftell(fp);
|
|
||||||
rewind(fp);
|
|
||||||
|
|
||||||
file = calloc(1, lSize + 1);
|
|
||||||
if (!file) {
|
|
||||||
fclose(fp);
|
|
||||||
fputs("memory allocation fail when reading file", stderr);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (1!=fread(file, lSize, 1, fp)) {
|
|
||||||
fclose(fp);
|
|
||||||
free(file);
|
|
||||||
fputs("couldn't read entire file", stderr);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// we done
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
if (argc == 1) {
|
if (argc == 1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user