Skip to content

Commit

Permalink
Merge pull request #5 from punixcorn/_free
Browse files Browse the repository at this point in the history
Makefile changes, Github workflow updated
  • Loading branch information
punixcorn authored Aug 7, 2024
2 parents 685c110 + 547e2fe commit 7cecaa6
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: make
run: make make_check
run: make workflow-test
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ a.out
peda-session-main.txt
*/**/*.h.gch
*/*.h.gch
lib/
static_lib/
lib/*
static_lib/*
build/*
shared_lib/*

87 changes: 57 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,42 +1,69 @@
FLAGS= -std=c11 -ggdb -w
# source files from include/
c_include_source_files := $(shell find include/* -name "*.c")
c_include_header_files := $(shell find include/* -name "*.h")
# source files from src/
c_source_files := $(shell find src/* -name "*.c")
c_header_files := $(shell find src/* -name "*.h")
# shared so files
#shared_libs = $(shell find shared_lib/* -name "*.so")
# Compile flags
FLAGS= -std=c11 -ggdb -w -nostdlib
# input args to main ( set at runtime )
args=


# uses shared files
shared: lib src/main.c
@./scripts/check_bin.sh
@./scripts/check_lib.sh
gcc $(FLAGS) -nostdlib lib/* include/src/_lib.c src/main.c -o bin/main
@# adding _lib.c because undefined refernce to _start();
# make all so files and build main binary with it
bootstrap_shared_lib:
@mkdir -p build shared_lib
@echo -e "[\e[42m Building so files... \e[0m]"
@$(foreach file ,$(c_include_source_files),gcc -shared $(file) -o $(patsubst include/src/%.c, shared_lib/%.so, $(file)) && ) true
@echo -e "\e[32m[+]\e[0m Done."

static: static_lib/* src/main.c
make static_lib
@./scripts/check_bin.sh
gcc -static -w -nostdlib $(FLAGS) src/main.c static_lib/no_libc.a -o bin/main
.PHONY: shared_lib
shared_lib: bootstrap_shared_lib $(c_include_source_files) $(c_source_files) $(c_header_files)
@echo -e "\e[32m[+]\e[0m Linking File..."
gcc ${FLAGS} $(shell find shared_lib/* -name "*.so") include/src/_lib.c ${c_source_files} -o build/main
@echo -e "\e[32m[+]\e[0m Done."

# uses .c files
main: include/**/* src/main.c
@./scripts/check_bin.sh
gcc $(FLAGS) -nostdlib include/src/* src/* -o bin/main

# runs every check
make_check:
@echo "=============================================="
make main
.PHONY: static_lib
static_lib: $(c_include_source_files) $(c_include_header_files) $(c_source_files) $(c_include_files)
@mkdir -p build/
@echo -e "[\e[42m Building Static lib... \e[0m]"
cd static_lib && make
@echo -e "\e[32m[+]\e[0m Linking File..."
gcc -nostdlib -L. ./src/main.c -l:./static_lib/no_libc.a -o build/main
@echo -e "\e[32m[+]\e[0m Done."

# creates a static lib
static_lib_:
cd static_lib && make all

test:
@echo -e "\e[43m[ TESTING BUILDS \e[0m]"
@echo -e "\e[33m[#]\e[0m TESTING : LINKING C FILES"
make all
@echo -e "\e[33m[#]\e[0m TESTING : CREATING SO LIBS"
make shared_lib
@echo -e "\e[33m[#]\e[0m TESTING : CREATING STATIC LIBS"
make static_lib
@echo -e "\e[32m[+]\e[0m Done."

workflow-test:
@echo -e "\e[43m[ TESTING WORKFLOW BUILD \e[0m]"
@echo -e "\e[33m[#]\e[0m TESTING : LINKING C FILES"
make all

# uses .c files
.PHONY: all
all: $(c_include_source_files) $(c_include_header_files) $(c_source_files) $(c_include_files)
@echo -e "[\e[42m Building Binary file... \e[0m]"
@mkdir -p build/
gcc $(FLAGS) ${c_include_source_files} ${c_source_files} -o build/main
@echo -e "\e[32m[+]\e[0m Done."

.PHONY: run
run:
@echo "=============================================="
@./bin/main $(args)
@./build/main $(args)
@echo "=============================================="
make clean

clean:
@echo "Cleaning..."
@touch bin/t lib/a
@rm bin/* 2>/dev/null
@rm lib/* 2>/dev/null
@echo "Done"
rm -rf build/* 2>/dev/null
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ make run # run's binary
### Run tests

```sh
make make_check
make test
```

### Targets in Makefile

- `make_check` = compile lib in all formats (**.so**,**.a**,**.c**) for testing
- `static` = creats a static lib in `static_lib/` dir and links to `src/main.c`
- `static_lib_` = creates static lib `no_libc.a` in `static_lib/` dir
- `shared` = creates shared objects (**.so**) in `lib/` ( it is the default target )
- `main` = uses raw c files in `include/src/` and links them to `src/main.c`
- `test` = compile lib in all formats (**.so**,**.a**,**.c**) for testing
- `static_lib` = creates static lib `no_libc.a` in `static_lib/` dir
- `shared_lib` = creates shared objects (**.so**) in `shared_lib/` ( it is the default target )
- `all` = uses raw c files in `include/src/` and links them to `src/main.c`

---

Expand All @@ -54,15 +53,19 @@ make make_check
---

# DIRECTORIES
## libc source codes
- `include/` = header include files
- `include/src/` = source include files for the header include files
- `src/` = source file(s), that's where `main.c` is located ( Program Entry )

- `src/` = source file(s), that's where `main.c` is located
- `include/` = header files & source files dir, all used for the header files
- `include/src/` = source files for the header files
- `iib/` = compiled shared object files ( `.so` files )
- `scripts/` = scripts to compile shared files and check shared files ( used in makefile )
## Compiled source codes
- `shared_iib/` = compiled shared object files ( `.so` files )
- `static_lib/` = contains `no_libc.a` for static linking
- `bin` = ouput binary is placed

## others
- `scripts/` ( no longer in use ) = scripts to compile shared files and check shared files ( used in old makefile )

---

### Available Functions
Expand Down
26 changes: 15 additions & 11 deletions include/_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,50 @@ __attribute__((__format__(__printf__, 1, 2))) void _printf(

/* read until newline from STDIN into buf */
void _scanline(char* __restrict __buf);
/* read size bytes from STDIN to buf */

/* read size bytes from STDIN to __buf */
__attribute__((nonnull(1))) void _scan(char* __restrict __buf,
const unsigned int size);

/* read from STDIN untill whitespace */
char* __readword(char* buf);

/* read formmated output from STDIN */
__attribute__((nonnull(1), __format__(__printf__, 1, 2))) void _scanf(
__attribute__((nonnull(1), __format__(__scanf__, 1, 2))) void _scanf(
const char* __restrict __fmt, ...);

/* np [d*] */
__attribute__((nonnull(1), __format__(__printf__, 2, 3))) int _sscanf(
__attribute__((nonnull(1), __format__(__scanf__, 2, 3))) int _sscanf(
const char* __restrict __s, const char* __restrict __format, ...);

/* write formmated output to S */
__attribute__((nonnull(1), __format__(__printf__, 2, 3))) int _sprintf(
char* __restrict __s, char* __restrict __fmt, ...);

/* return formmated string fmt */
[[nodiscard("returns a formatted string")]]
__attribute__((nonnull(1), __format__(__printf__, 1,
2))) char* _format(char* __restrict __fmt,
...);

[[nodiscard("returns a formatted string")]] __attribute__((
nonnull(1), __format__(__printf__, 1, 2))) char*
_format(char* __restrict __fmt, ...);

/* write formmated output to S */
/* write formmated output to __s */
__attribute__((nonnull(1), __format__(__printf__, 3, 4))) int _snprintf(
char* __restrict __s, size_t __maxlen, const char* __restrict __format,
...);

/* write character to stdout */
/* write to stream */
__attribute__((nonnull(2))) int _fputc(int __c, FILE* __stream);
/* write to FILE stream */
__attribute__((nonnull(2))) int _putc(int __c, FILE* __stream);

/* write character to stdout */
int _putchar(int __c);

/* Read a character from stdin. */
/* Read a character from stream. */
__attribute__((nonnull(1))) int _fgetc(FILE* __stream);
__attribute__((nonnull(1))) int _getc(FILE* __stream);
[[nodiscard("returns a char from stream")]] int _getchar(void);
__attribute__((__format__(__printf__, 2, 3))) int _fscanf(
__attribute__((__format__(__scanf__, 2, 3))) int _fscanf(
FILE* __restrict __stream, const char* __restrict __format, ...);

#endif
5 changes: 5 additions & 0 deletions include/_stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#ifndef __STACK_CHK_FAIL__
#define __STACK_CHK_FAIL__
int __stack_chk_fail(void);
#endif
4 changes: 3 additions & 1 deletion include/src/_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void _exit(int exitcode) {
"movl %1, %%eax;"
"syscall" ::"r"(exitcode),
"r"(SYSEXIT));
__builtin_unreachable();
}

/*
Expand All @@ -17,7 +18,8 @@ void _exit(int exitcode) {
* RETURN VALUE FROM MAIN() WILL BE PASSED INTO SYSEXIT()
*/
void _start() {
int main_return = main();
/* -127 -> 127, No need to use int , char will do */
signed char main_return = main();
_exit(main_return);
__builtin_unreachable();
}
Expand Down
4 changes: 1 addition & 3 deletions include/src/_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* :(.text+0x102): undefined reference to `__stack_chk_fail'
* collect2: error: ld returned 1 exit status
*/
#include "../_stack.h"

#ifndef __STACK_CHK_FAIL__
#define __STACK_CHK_FAIL__
int __stack_chk_fail(void) { return 0; };
#endif
2 changes: 1 addition & 1 deletion scripts/check_lib.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
files=$(ls lib/)
files=$(/bin/ls lib/)

if [ -z "$files" ];then
./scripts/shared.sh
Expand Down
69 changes: 69 additions & 0 deletions scripts/oldmakefile.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
c_include_source_files := $(shell find include/* -name "*.c")
c_include_header_files := $(shell find include/* -name "*.h")
c_source_files := $(shell find src/* -name "*.c")
c_header_files := $(shell find src/* -name "*.h")

shared_libs = $(shell find shared_lib/* -name "*.so")

shared_libs_output_files := $(patsubst include/src/%.c, shared_lib/%.so, $(c_include_source_files))
FLAGS= -std=c11 -ggdb -w -nostdlib -nostartfiles
args=

koo:
@echo $(shared_libs_output_files)

loop:
$(foreach file ,$(c_include_source_files),gcc -shared $(file) -o $(patsubst include/src/%.c, shared_lib/%.so, $(file)) && ) true
echo:

@echo $(output_shared_object_files_name:_=lib_)
@echo ${c_include_source_files}
@echo ${c_include_header_files}
@echo ${c_source_files}
@echo ${c_header_files}
@echo ${shared_libs}


# uses shared files
shared: src/main.c
@mkdir -p build lib
@./scripts/check_lib.sh
gcc $(FLAGS) -nostdlib lib/* include/src/_lib.c src/main.c -o build/main
@# adding _lib.c because undefined refernce to _start();

shared_2: $(c_include_source_files) $(c_source_files) $(shared_libs)
@mkdir -p build shared_lib
@$(foreach file ,$(c_include_source_files),gcc -shared $(file) -o $(patsubst include/src/%.c, shared_lib/%.so, $(file)) && ) true
gcc ${FLAGS} ${shared_libs} include/src/_lib.c ${c_source_files} -o build/main


static: static_lib/* src/main.c
make static_lib
@mkdir -p build/
gcc -static -w -nostdlib $(FLAGS) src/main.c static_lib/no_libc.a -o build/main

# uses .c files
main: include/**/* src/main.c
@mkdir -p build/
gcc $(FLAGS) -nostdlib include/src/* src/* -o build/main

# runs every check
make_check:
$(info "==============================================")
make main

# creates a static lib
static_lib_:
cd static_lib && make all

run:
@echo "=============================================="
@./build/main $(args)
@echo "=============================================="
make clean

clean:
@echo "Cleaning..."
@rm -rf build/ 2>/dev/null
@rm -rf lib/ 2>/dev/null
@echo "Done"
7 changes: 3 additions & 4 deletions scripts/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ green="\e[32m"
red="\e[35m"
bold="\e[1m"

for i in $(/bin/ls include/src/) ;do

for file in $(/bin/ls include/src/) ;do
printf "${bold}compiling${normal} ${green}$i${normal} => "
name=$( sed 's/\.c//g' <<< $i )
name=$( sed 's/\.c//g' <<< $file )
printf "${bold}${red}${name}lib.so${normal}\n"
gcc -shared include/src/$i -o lib/lib${name}.so
gcc -shared include/src/$file -o lib/lib${name}.so
done
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ int main() {
_println("Memory not freeed");
}

int i = 0;
_scanf("%d", &i);
_printf("%d is i", i);
return 0;
}
14 changes: 9 additions & 5 deletions static_lib/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
all: *.o
ar rvs no_libc.a *.o
rm *.o
c_include_source_files := $(shell find ../include/ -name "*.c")

*.o:
gcc -c ../include/src/*
no_libc.a: *.o
@echo -e "[\e[42m Archiving... \e[0m]"
ar rvs $@ $?
rm $?

*.o: ${c_include_source_files}
@echo -e "[\e[42m Building... \e[0m]"
gcc -c $?

clean:
@touch a.o a.a
Expand Down
Binary file modified static_lib/no_libc.a
Binary file not shown.

0 comments on commit 7cecaa6

Please sign in to comment.