Skip to content

Commit

Permalink
opened build system
Browse files Browse the repository at this point in the history
  • Loading branch information
cielavenir committed Jun 14, 2020
1 parent 2f3ca74 commit cc33469
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 89 deletions.
187 changes: 187 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
BUILD := build

ifneq ($(BUILD),$(notdir $(CURDIR)))

TARGET ?= cielbox

SOURCES += applet applet_stl applet/hash lib lib/cryptonite lib/libmshlsplash lib/zlib lib/zopfli lib/libdeflate lib/lz4 lib/zstd lib/libbzip2 lib/popt .
INCLUDES +=
DATA += data

#--- set toolchain name
ifneq ($(CLANG),)
export CC := $(PREFIX)clang
#export CXX := $(PREFIX)clang++ -stdlib=libc++
export CXX := $(PREFIX)clang++
else ifneq ($(OPEN64),)
export CC := $(PREFIX)opencc
export CXX := $(PREFIX)openCC
else
export CC := $(PREFIX)gcc
export CXX := $(PREFIX)g++
endif

#export AS := $(PREFIX)as
#export AR := $(PREFIX)ar
#export OBJCOPY := $(PREFIX)objcopy
export STRIP := $(PREFIX)strip

#--- set path
### relative path from build
#LIBS +=
export LIBS

LIBDIRS :=
export LIBDIRS

export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)

export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)

#--- flags
#ARCH :=
CFLAGS := -Wall -Wno-parentheses \
-Wno-sign-compare -Wno-unused-parameter -Wno-unused-value \
-O2 -fomit-frame-pointer -ffast-math \
-DNO_VIZ -DHAVE_INTERNAL -D_LIBCPP_NO_EXCEPTIONS \
$(ARCH)

ifeq ($(WIN32),)
#enable 7ZIP_ST in non-Win32
CFLAGS += -D_7ZIP_ST
endif
ifneq ($(CRYPTOPP),)
SOURCES += lib/cryptopp
CFLAGS += -DCRYPTOPP=1
LIBS += -lcryptopp
endif


#-DNO_VIZ for zlib.

CFLAGS += $(INCLUDE)
CXXFLAGS := $(CFLAGS) -fno-exceptions
#lovely hack...
#bah -Wno-pointer-sign must be stripped for iPodLinux
#-std=c99 breaks fileno() on Linux...
CFLAGS += -std=gnu99
CFLAGS += -Wno-pointer-sign -Wno-unused-result

ASFLAGS := $(ARCH)
LDFLAGS = $(ARCH) $(LDF) -O2
#-Wl,-Map,$(notdir $*.map)

ifneq ($(USTL),)
#you need to give libustl path. It must be linked statically.
CXXFLAGS += -DUSTL
LIBS += -lsupc++
ifneq ($(WIN32),)
LDFLAGS += -static-libgcc
endif
else
CXXFLAGS += -fno-rtti
ifneq ($(WIN32),)
LDFLAGS += -static-libgcc -static-libstdc++
endif
endif

ifneq ($(NOZLIBNG),)
CFLAGS += -DNOZLIBNG
else
SOURCES += lib/zlib-ng lib/zlib-ng/arch/arm
endif

ifneq ($(ZLIBNG_X86),)
### some very old gcc does not have immintrin.h; need to add flag...
SOURCES += lib/zlib-ng/arch/x86
CFLAGS += -DX86_CPUID -DX86_QUICK_STRATEGY
endif

ifneq ($(NOIGZIP),)
CFLAGS += -DNOIGZIP
else
LIBS += ../lib/isa-l/bin/isa-l.a
endif

export CFLAGS
export CXXFLAGS
export LDFLAGS

export BIN := $(CURDIR)/$(TARGET)$(SUFF)
export DEPSDIR := $(CURDIR)/$(BUILD)

#export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
# $(foreach dir,$(DATA),$(CURDIR)/$(dir))
export VPATH := $(CURDIR)

CFILES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.c))
CPPFILES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.cpp))
SFILES := $(foreach dir,$(SOURCES),$(wildcard $(dir)/*.s))
#BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

ifeq ($(strip $(CPPFILES)),)
export LD := $(CC)
else
ifneq ($(USTL),)
#Oh dear: undefined reference to std::__throw_out_of_range_fmt
#No proper fix yet...
#export LD := $(CXX)
export LD := $(CC)
else
#gcc doesn't understand -static-libstdc++...
export LD := $(CXX)
endif
endif

export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)

.PHONY: $(BUILD) clean

$(BUILD):
@[ -d $@ ] || mkdir -p $@
@[ "$(NOIGZIP)" != "" ] || CC=$(PREFIX)gcc CFLAGS="$(subst -flto,,$(CFLAGS))" make -C lib/isa-l -f Makefile.unx lib programs/igzip
@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile -j4

clean:
@#echo clean ...
@make -C lib/isa-l -f Makefile.unx clean
@rm -fr $(BUILD)

#---------------------------------------------------------------------------------
else

#%.a:
# @#echo $(notdir $@)
# @rm -f $@
# @$(AR) -rc $@ $^

lib/cryptopp/%.o: RTTI:=-frtti -fexceptions

%.o: %.cpp
@#echo $(notdir $<)
@[ -d $@ ] || mkdir -p $(dir $@)
@$(CXX) -MMD -MP -MF $(DEPSDIR)/$*.d $(CXXFLAGS) $(RTTI) -c $< -o $@ $(ERROR_FILTER)

%.o: %.c
@#echo $(notdir $<)
@[ -d $@ ] || mkdir -p $(dir $@)
@$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@ $(ERROR_FILTER)

%.o: %.s
@#echo $(notdir $<)
@[ -d $@ ] || mkdir -p $(dir $@)
@$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d -x assembler-with-cpp $(ASFLAGS) -c $< -o $@ $(ERROR_FILTER)

DEPENDS := $(OFILES:.o=.d)

$(BIN): $(OFILES)
@#echo linking $(notdir $@)
@$(LD) $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@
@if ! echo "$(LDFLAGS)" | grep -- -shared; then $(STRIP) $@; fi

-include $(DEPENDS)

endif
120 changes: 68 additions & 52 deletions bgzf_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ __attribute__((destructor)) void finish(){
if(loaded7z)lzmaClose7z();
}

int bgzf_compress(void *_dst, size_t *_dlen, const void *src, size_t slen, int level){
static int method=-1;
static int level=-1;

int bgzf_compress(void *_dst, size_t *_dlen, const void *src, size_t slen, int level_unused){
if(!slen){
if(*_dlen<28)return -1;
*_dlen=28;
Expand All @@ -47,59 +50,65 @@ int bgzf_compress(void *_dst, size_t *_dlen, const void *src, size_t slen, int l
return 0;
}

int method=DEFLATE_ZLIB;
const char* _smethod=getenv("BGZF_METHOD");
if(_smethod&&*_smethod){
char *smethod=alloca(strlen(_smethod)+1);
strcpy(smethod,_smethod);
//fputs(smethod,stderr);fputs("\n",stderr);
//set level
int _level=-1;
int l=strlen(smethod);
int i=l-1;
for(;i>=0 && '0'<=smethod[i]&&smethod[i]<='9';i--){
if(_level<0)_level=0;
_level*=10;
_level+=smethod[i]-'0';
}
if(_level>=0)level=_level;
//set method
smethod[i+1]=0;
if(!strcasecmp(smethod,"zlib")){
method=DEFLATE_ZLIB;
}
if(!strcasecmp(smethod,"7zip") || !strcasecmp(smethod,"7-zip")){
method=DEFLATE_7ZIP;
}
if(!strcasecmp(smethod,"zopfli")){
method=DEFLATE_ZOPFLI;
}
if(!strcasecmp(smethod,"miniz")){
method=DEFLATE_MINIZ;
}
if(!strcasecmp(smethod,"slz") || !strcasecmp(smethod,"libslz")){
method=DEFLATE_SLZ;
}
if(!strcasecmp(smethod,"libdeflate")){
method=DEFLATE_LIBDEFLATE;
}
if(!strcasecmp(smethod,"zlibng")){
method=DEFLATE_ZLIBNG;
}
if(!strcasecmp(smethod,"igzip")){
method=DEFLATE_IGZIP;
if(method==-1){
method=DEFLATE_ZLIB;
const char* _smethod=getenv("BGZF_METHOD");
if(_smethod&&*_smethod){
char *smethod=alloca(strlen(_smethod)+1);
strcpy(smethod,_smethod);
//fputs(smethod,stderr);fputs("\n",stderr);
//set level
int _level=-1;
int l=strlen(smethod);
int i=l-1;
for(;i>=0 && '0'<=smethod[i]&&smethod[i]<='9';i--){
if(_level<0)_level=0;
_level*=10;
_level+=smethod[i]-'0';
}
if(_level>=0)level=_level;
//set method
smethod[i+1]=0;
if(!strcasecmp(smethod,"zlib")){
method=DEFLATE_ZLIB;
}
if(!strcasecmp(smethod,"7zip") || !strcasecmp(smethod,"7-zip")){
method=DEFLATE_7ZIP;
}
if(!strcasecmp(smethod,"zopfli")){
method=DEFLATE_ZOPFLI;
}
if(!strcasecmp(smethod,"miniz")){
method=DEFLATE_MINIZ;
}
if(!strcasecmp(smethod,"slz") || !strcasecmp(smethod,"libslz")){
method=DEFLATE_SLZ;
}
if(!strcasecmp(smethod,"libdeflate")){
method=DEFLATE_LIBDEFLATE;
}
if(!strcasecmp(smethod,"zlibng")){
method=DEFLATE_ZLIBNG;
}
if(!strcasecmp(smethod,"igzip")){
method=DEFLATE_IGZIP;
}
if(!strcasecmp(smethod,"cryptopp")){
method=DEFLATE_CRYPTOPP;
}
}
}

if(level<0){
if(method==DEFLATE_ZLIB)level=6;
if(method==DEFLATE_7ZIP)level=2;
if(method==DEFLATE_ZOPFLI)level=1;
if(method==DEFLATE_MINIZ)level=1;
if(method==DEFLATE_SLZ)level=1;
if(method==DEFLATE_LIBDEFLATE)level=6;
if(method==DEFLATE_ZLIBNG)level=6;
if(method==DEFLATE_IGZIP)level=1;
if(level<0){
if(method==DEFLATE_ZLIB)level=6;
if(method==DEFLATE_7ZIP)level=2;
if(method==DEFLATE_ZOPFLI)level=1;
if(method==DEFLATE_MINIZ)level=1;
if(method==DEFLATE_SLZ)level=1;
if(method==DEFLATE_LIBDEFLATE)level=6;
if(method==DEFLATE_ZLIBNG)level=6;
if(method==DEFLATE_IGZIP)level=1;
if(method==DEFLATE_CRYPTOPP)level=6;
}
}

//compress
Expand Down Expand Up @@ -171,6 +180,13 @@ int bgzf_compress(void *_dst, size_t *_dlen, const void *src, size_t slen, int l
return 1;
}
}
if(method==DEFLATE_CRYPTOPP){
int r=cryptopp_deflate(dst,&dlen,(const unsigned char*)src,slen,level);
if(r){
fprintf(stderr,"cryptopp_deflate %d\n",r);
return 1;
}
}
*_dlen=dlen+25;
memcpy(_dst,"\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff\x06\0BC\x02\x00",16);
write16(_dst+16,*_dlen);
Expand Down
45 changes: 8 additions & 37 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
#!/bin/sh

# CC=arm-linux-androideabi-gcc CFLAGS="-mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon" arch=noarch host_cpu=base_aliases
# CC=i686-w64-mingw32-gcc LIBS="-static-libgcc -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive,-Bdynamic -lole32 -loleaut32" CFLAGS="-mmmx -msse -msse2" arch=noarch host_cpu=base_aliases ZLIBNG_X86=1
# CC=x86_64-w64-mingw32-gcc LIBS="-static-libgcc -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive,-Bdynamic -lole32 -loleaut32" CFLAGS="-march=core2 -mfpmath=sse -mmmx -msse -msse2 -msse3 -mno-ssse3" arch=mingw host_cpu=x86_64 ZLIBNG_X86=1

SOURCES_7razf="applet/7razf_testdecode.c"
SOURCES_7dictzip="applet/7razf_testdecode.c"
SOURCES_7gzinga="lib/memmem.c"

if [ -z "${CC}" ]; then
CC="gcc"
if [ -z "${CFLAGS}" ]; then
CFLAGS="-march=native"
fi
fi
if [ -z "${LIBS}" ]; then
LIBS="-lm -ldl -pthread"
fi
if [ ! -z "${ZLIBNG_X86}" ]; then
ZLIBNG_X86="-DX86_CPUID -DX86_QUICK_STRATEGY lib/zlib-ng/arch/x86/*.c"
fi
SOURCES_LIB="lib/zopfli/*.c lib/popt/*.c lib/zlib/*.c lib/zlib-ng/*.c lib/zlib-ng/arch/arm/*.c ${ZLIBNG_X86} lib/memstream.c lib/zlibutil.c lib/zlibutil_zlibng.c lib/zlibutil_igzip.c lib/miniz.c lib/slz.c lib/libdeflate/deflate_compress.c lib/libdeflate/deflate_decompress.c lib/libdeflate/aligned_malloc.c lib/libdeflate/*/cpu_features.c lib/lzmasdk.c"

mkdir -p bin

make -C lib/isa-l -f Makefile.unx lib
for i in 7bgzf 7razf 7gzip 7gzinga 7png 7migz 7dictzip 7ciso 7daxcr zlibrawstdio zlibrawstdio2
do
# ZopfliCalculateEntropy uses log, which is implemented in libm.
${CC} -O2 -std=gnu99 ${CFLAGS} -DSTANDALONE -o bin/${i} applet/${i}.c $(eval echo '$'SOURCES_${i}) ${SOURCES_LIB} lib/isa-l/bin/isa-l.a ${LIBS}
done
make -C lib/isa-l -f Makefile.unx clean

export CFLAGS="${CFLAGS} -fPIC"
make -C lib/isa-l -f Makefile.unx lib
${CC} -O2 -std=gnu99 ${CFLAGS} -shared -o bin/7bgzf.so bgzf_compress.c ${SOURCES_LIB} lib/isa-l/bin/isa-l.a ${LIBS}
make -C lib/isa-l -f Makefile.unx clean
#make clean
echo "Self"
ZLIBNG_X86=1 ARCH="-march=native -mfpmath=sse -pthread" LIBS="-ldl -pthread" make
#make clean

#On Debian, use 3.08-2 or later.
#upx --best --lzma cielbox
# cielbox.exe
Loading

0 comments on commit cc33469

Please sign in to comment.