From f55e7834ba8b89cdca452ca8cb83299c7a890149 Mon Sep 17 00:00:00 2001 From: Robert Xiao Date: Thu, 6 Jul 2023 20:12:36 -0700 Subject: [PATCH] Replace javah by javac -h, only write new constant files if something changes. The const_generator changes help to ensure that e.g. Java rebuilds don't keep rebuilding everything. --- bindings/const_generator.py | 18 ++++++++++++++++-- bindings/java/.gitignore | 1 - bindings/java/Makefile | 8 ++------ bindings/java/pom.xml | 6 ++++++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/bindings/const_generator.py b/bindings/const_generator.py index 40e02048..982b48f4 100644 --- a/bindings/const_generator.py +++ b/bindings/const_generator.py @@ -159,8 +159,9 @@ def gen(lang): templ = template[lang] for target in include: prefix = templ[target] - outfile = open(templ['out_file'] %(prefix), 'wb') # open as binary prevents windows newlines - outfile.write((templ['header'] % (prefix)).encode("utf-8")) + outfn = templ['out_file'] % prefix + outfile = open(outfn + ".tmp", 'wb') # open as binary prevents windows newlines + outfile.write((templ['header'] % prefix).encode("utf-8")) if target == 'unicorn.h': prefix = '' with open(os.path.join(INCL_DIR, target)) as f: @@ -278,6 +279,19 @@ def gen(lang): outfile.write((templ['footer']).encode("utf-8")) outfile.close() + if os.path.isfile(outfn): + with open(outfn, "rb") as infile: + cur_data = infile.read() + with open(outfn + ".tmp", "rb") as infile: + new_data = infile.read() + if cur_data == new_data: + os.unlink(outfn + ".tmp") + else: + os.unlink(outfn) + os.rename(outfn + ".tmp", outfn) + else: + os.rename(outfn + ".tmp", outfn) + def main(): lang = sys.argv[1] if lang == "all": diff --git a/bindings/java/.gitignore b/bindings/java/.gitignore index b2a1df87..2f7896d1 100644 --- a/bindings/java/.gitignore +++ b/bindings/java/.gitignore @@ -1,2 +1 @@ target/ -unicorn_Unicorn.h diff --git a/bindings/java/Makefile b/bindings/java/Makefile index 9dae6098..aa3fe7d1 100644 --- a/bindings/java/Makefile +++ b/bindings/java/Makefile @@ -26,14 +26,11 @@ CFLAGS=-fPIC LDFLAGS=-shared -fPIC # May also use -lunicorn to dynamically link against the installed unicorn LIBS=../../build/libunicorn.a -INCS=-I$(JAVA_INC) -I$(JAVA_PLATFORM_INC) -I$(UNICORN_INC) +INCS=-I target/headers -I$(JAVA_INC) -I$(JAVA_PLATFORM_INC) -I$(UNICORN_INC) OBJS=unicorn_Unicorn.o -unicorn_Unicorn.h: src/main/java/unicorn/Unicorn.java - javah -cp src/main/java -o $@ unicorn.Unicorn - -unicorn_Unicorn.o: unicorn_Unicorn.c unicorn_Unicorn.h +unicorn_Unicorn.o: unicorn_Unicorn.c target/headers/unicorn_Unicorn.h $(CC) -O2 -Wall -Wextra -Wno-unused-parameter -c $(CFLAGS) $(INCS) $< -o $@ libunicorn_java$(LIB_EXT): $(OBJS) @@ -41,7 +38,6 @@ libunicorn_java$(LIB_EXT): $(OBJS) clean: rm -f libunicorn_java$(LIB_EXT) - rm -f unicorn_Unicorn.h rm -f $(OBJS) .PHONY: all clean diff --git a/bindings/java/pom.xml b/bindings/java/pom.xml index fa5da8f2..686f7b66 100644 --- a/bindings/java/pom.xml +++ b/bindings/java/pom.xml @@ -33,6 +33,12 @@ org.apache.maven.plugins maven-compiler-plugin 3.11.0 + + + -h + target/headers + +