From f9e1e76011236f05d6e2c3b62e2ae2969c64f808 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 24 May 2025 17:38:59 +0200 Subject: Adjusted files for cross-compilation on Linux --- Makefile | 93 +++++++++++++++++++++++++++++++++++++++++++++++------ include/tolmacdef.h | 1 + src/tolsac.c | 8 ++--- structure.txt | 29 +++-------------- tests/Example.csv | 14 ++++---- 5 files changed, 99 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index d192768..dc5f750 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,104 @@ CC=gcc +# Cross-compilation (only used on Linux) +MINGW_CC=x86_64-w64-mingw32-gcc + #Base Directories SRCDIR=src BINDIR=bin +BINDIR_WIN=bin/win INCDIR=include #Common flags -CFLAGS=-I$(INCDIR) -O2 -march=native -flto #-ffast-math +CFLAGS_BASE=-I$(INCDIR) -O2 -flto +CFLAGS_NATIVE=$(CFLAGS_BASE) -march=native #-ffast-math +CFLAGS_PORTABLE=$(CFLAGS_BASE) #-ffast-math (no -march=native for portability) + +# OS Detection +ifeq ($(OS),Windows_NT) + # Windows host + EXE_EXT=.exe + MKDIR=mkdir + RM=del /Q + LDFLAGS= + TREE_CMD=powershell -Command "(Get-Item .).Name | Set-Content structure.txt; (tree /a /f | Select-Object -Skip 3) | Add-Content structure.txt" + + # On Windows: compile Windows binaries without -march=native for portability + NATIVE_CC=$(CC) + NATIVE_CFLAGS=$(CFLAGS_PORTABLE) + NATIVE_LDFLAGS=$(LDFLAGS) + NATIVE_EXT=.exe +else + # Linux host + EXE_EXT= + MKDIR=mkdir -p + RM=rm -f + LDFLAGS=-lm + TREE_CMD=basename $(PWD) > structure.txt; tree -a -F >> structure.txt 2>/dev/null || (find . -type f -name "*.c" -o -name "*.h" -o -name "Makefile" | sort >> structure.txt) + + # On Linux: compile Linux binaries with -march=native for performance + NATIVE_CC=$(CC) + NATIVE_CFLAGS=$(CFLAGS_NATIVE) + NATIVE_LDFLAGS=$(LDFLAGS) + NATIVE_EXT= +endif + #tolutils -TOL_SRCS = $(wildcard $(SRCDIR)/*.c) -TOL_EXES = $(patsubst $(SRCDIR)/%.c, $(BINDIR)/%.exe, $(TOL_SRCS)) +TOL_SRCS = $(wildcard $(SRCDIR)/*.c) +TOL_EXES_NATIVE = $(patsubst $(SRCDIR)/%.c, $(BINDIR)/%$(NATIVE_EXT), $(TOL_SRCS)) +TOL_EXES_WIN = $(patsubst $(SRCDIR)/%.c, $(BINDIR_WIN)/%.exe, $(TOL_SRCS)) # Setup target setup: - mkdir -p $(BINDIR) $(INCDIR) + $(MKDIR) $(BINDIR) $(INCDIR) +setup-cross: + $(MKDIR) $(BINDIR) $(BINDIR_WIN) $(INCDIR) + +# Default: build for current platform all: setup tolsac structure -tolsac: $(TOL_EXES) -$(BINDIR)/%.exe: $(SRCDIR)/%.c - $(CC) $< $(CFLAGS) -o $@ +# Build for current platform (native) +tolsac: $(TOL_EXES_NATIVE) + +# Native compilation rule +$(BINDIR)/%$(NATIVE_EXT): $(SRCDIR)/%.c + $(NATIVE_CC) $< $(NATIVE_CFLAGS) -o $@ $(NATIVE_LDFLAGS) + +# Cross-compilation targets (Linux host only) +ifneq ($(OS),Windows_NT) +tolsac-win: setup-cross $(TOL_EXES_WIN) +tolsac-all: tolsac tolsac-win +rebuild-win: clean tolsac-win +rebuild-all: clean tolsac-all + +$(BINDIR_WIN)/%.exe: $(SRCDIR)/%.c + $(MINGW_CC) $< $(CFLAGS_PORTABLE) -o $@ +else +# On Windows, provide helpful messages for cross-compilation targets +tolsac-win: + @echo "Cross-compilation to Windows not needed on Windows host. Use 'make tolsac' instead." + +tolsac-all: tolsac + @echo "On Windows host: built Windows binaries (cross-compilation not applicable)." + +rebuild-win: rebuild + @echo "On Windows host: rebuilt Windows binaries." + +rebuild-all: rebuild + @echo "On Windows host: rebuilt Windows binaries." +endif clean: - rm -f $(BINDIR)/* - rm -f structure.txt + $(RM) $(BINDIR)/* +ifneq ($(OS),Windows_NT) + $(RM) $(BINDIR_WIN)/* +endif + $(RM) structure.txt rebuild: clean all # Generate project structure structure: - @powershell -Command "(Get-Item .).Name | Set-Content structure.txt; (tree /a /f | Select-Object -Skip 3) | Add-Content structure.txt" + @$(TREE_CMD) + +.PHONY: all setup setup-cross tolsac tolsac-win tolsac-all clean rebuild rebuild-win rebuild-all structure diff --git a/include/tolmacdef.h b/include/tolmacdef.h index d8a5543..b1d6bc0 100644 --- a/include/tolmacdef.h +++ b/include/tolmacdef.h @@ -16,6 +16,7 @@ #include #include #include +#include /* Type definitions for consistent sizing across platforms. Idea taken from https://nullprogram.com/blog/2023/10/08/ (archive link: ) */ typedef uint8_t u8; typedef char16_t c16; diff --git a/src/tolsac.c b/src/tolsac.c index ee89921..7707a01 100644 --- a/src/tolsac.c +++ b/src/tolsac.c @@ -202,7 +202,7 @@ void display_help(void) { "50 is the number of iterations produced (not required for the " "grid and sensitivity samplings)\n" "-o Output.csv specifies the output path and filename.\n" - "If not provided, output will be named Sampled_[Input.csv] in the current directory.\n" + "If not provided, output will be named sampled_[Input.csv] in the current directory.\n" "If you are unsure how the input should look like, " "run the program with the -e option to generate an example " "input file.\n\n" @@ -224,7 +224,7 @@ void display_help(void) { // Function to generate example file void generate_example(void) { - FILE *writefile = fopen("Example.csv", "w"); + FILE *writefile = fopen("example.csv", "w"); if (!writefile) { printf("Error creating example file!\n"); return; @@ -239,7 +239,7 @@ void generate_example(void) { fprintf(writefile, "Standard Deviation, 0, 0, 1.3, 1.0\n"); fclose(writefile); - printf("Example.csv file generated!\n"); + printf("example.csv file generated!\n"); } // Optimized LHS interval generation that transposes for better cache locality @@ -357,7 +357,7 @@ i32 main(i32 argc, char *argv[]) { file_ptr = strrchr(rfilename, '/'); } - strcpy(wfilename, "Sampled_"); + strcpy(wfilename, "sampled_"); if (file_ptr) { strcat(wfilename, file_ptr + 1); } else { diff --git a/structure.txt b/structure.txt index 31f8b70..32a0398 100644 --- a/structure.txt +++ b/structure.txt @@ -1,25 +1,4 @@ -tolsac -| .clangd -| .gitignore -| compile_flags.txt -| COPYING -| COPYRIGHT -| Makefile -| README.md -| structure.txt -| -+---bin -| tolsac.exe -| -+---data -+---include -| tolmacdef.h -| -+---src -| tolsac.c -| -\---tests - Example.csv - hist.r - Sampled_Example.csv - +tolsac +./include/tolmacdef.h +./Makefile +./src/tolsac.c diff --git a/tests/Example.csv b/tests/Example.csv index 2c6ed66..dbe6c4c 100644 --- a/tests/Example.csv +++ b/tests/Example.csv @@ -1,7 +1,7 @@ -Iteration Number,Parameter 1 Name,Parameter 2 Name,Parameter 3 Name,Parameter 4 Name -Probability Density Function (U=Uniform; N=Normal/Gaussian; T=Truncated Normal),U,U,N,T -Nominal, 0, 0, 6, 5 -Max, 1, 0.1, 10, 7 -Min, -1, -0.1, 2, 3 -Mean,0.0, 0.0, 6.0, 5.0 -Standard Deviation, 0, 0, 1.3, 1.5 +Iteration Number,Parameter 1 Name,Parameter 2 Name,Parameter 3 Name,Parameter 4 Name +Probability Density Function (U=Uniform; N=Normal/Gaussian; T=Truncated Normal),U,U,N,T +Nominal, 0, 0, 6, 5 +Max, 1, 0.1, 10, 7 +Min, -1, -0.1, 2, 3 +Mean,0.0, 0.0, 6.0, 5.0 +Standard Deviation, 0, 0, 1.3, 1.0 -- cgit v1.2.3