J08nY / ecgen

Tool for generating Elliptic curve domain parameters

Home Page:https://neuromancer.sk/page/ecgen

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix Makefiles

noloader opened this issue · comments

I'm building the latest ecgen tarball against PARI/GP 2.13.0. PARI/GP 2.13.0 was released in January 2021. All warez have been installed into $HOME/ok2delete. PARI/GP is not located in /usr and friends.

There were lots of compile errors due to missing functions and implicit declarations. The problem was, the ecgen Makefiles were stomping on my CPPFLAGS, CFLAGS, LDFLAGS and LIBS. The user owns those flags and they should not be touched.

The fix is for ecgen to use its own flags, ECGEN_CFLAGS, ECGEN_LDFLAGS and ECGEN_LIBS, and then use them in the build recipe like:

 ifeq ($(DEBUG), 1)
-    CFLAGS += -DDEBUG -g -Og -Werror -pedantic
+    ECGEN_CFLAGS = -DDEBUG -g -Og -Werror -pedantic
 else ifeq ($(TEST), 1)
-	CFLAGS += -DNDEBUG --coverage -g -O0
+	ECGEN_CFLAGS = -DNDEBUG --coverage -g -O0
 else ifeq ($(FAST), 1)
     ifeq ($(CC), gcc)
-        CFLAGS += -DNDEBUG -O3 -march=native -pipe
+        ECGEN_CFLAGS = -DNDEBUG -O3 -march=native -pipe
     else
-        CFLAGS += -DNDEBUG -O3
+        ECGEN_CFLAGS = -DNDEBUG -O3
     endif
 else
-    CFLAGS += -DNDEBUG -O2
+    ECGEN_CFLAGS = -DNDEBUG -O2
 endif
 
-LDFLAGS = -L../lib/parson -L../lib/sha1 -L../lib/pari
+ECGEN_LDFLAGS = -L../lib/parson -L../lib/sha1 -L../lib/pari
 ifeq ($(STATIC), 1)
-	LIBS = -lrt -Wl,-Bstatic -lpari -Wl,-Bdynamic -lpthread -lparson -lsha1 -lm -lgmp -ldl
+	ECGEN_LIBS = -lrt -Wl,-Bstatic -lpari -Wl,-Bdynamic -lpthread -lparson -lsha1 -lm -lgmp -ldl
 else
-	LIBS = -lrt -lpari -lpthread -lparson -lsha1
+	ECGEN_LIBS = -lrt -lpari -lpthread -lparson -lsha1
 endif
 
-INCLUDES = -I. -I../lib
+ECGEN_INCLUDES = -I. -I../lib
 
 
-GIT_COMMIT = $(shell git rev-parse --short HEAD)
+GIT_COMMIT = $(shell git rev-parse --short HEAD 2>/dev/null)
 ifneq ($(GIT_COMMIT),)
-	CFLAGS += -DGIT_COMMIT=\"$(GIT_COMMIT)\"
+	ECGEN_CFLAGS += -DGIT_COMMIT=\"$(GIT_COMMIT)\"
 endif
...

 all: ecgen
 
 ecgen: ecgen.o $(ECGEN_OBJ)
-	$(CC) $(CFLAGS) $(INCLUDES) -o $@ $^ $(LDFLAGS) $(LIBS)
+	$(CC) $(strip $(CPPFLAGS) $(ECGEN_INCLUDES) $(ECGEN_CFLAGS) $(CFLAGS) -o) $@ $^ $(ECGEN_LDFLAGS) $(LDFLAGS) $(ECGEN_LIBS) $(LIBS)
 	mv ecgen ..
 
 %.o: %.c
-	$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
+	$(CC) $(strip $(CPPFLAGS) $(ECGEN_INCLUDES) $(ECGEN_CFLAGS) $(CFLAGS) -c -o) $@ $<

In the diff... if the user does nothing special, then all of the current ecgen defaults are used. If the user provides flags, like CPPFLAGS="-I /home/test/ok2delete/include" or LDFLAGS="-L /home/test/ok2delete/lib", then they are used during the build.

The call to $(strip ...) cleans up a GCC invocation by remove extraneous whitespace. It makes the output look nice:

/usr/bin/cc -I/home/test/ok2delete/include -DNDEBUG -I. -I../lib -DNDEBUG -O2 -DGIT_COMMIT=\"f8a65c5\" -Wall -c -o ecgen.o ecgen.c
/usr/bin/cc -I/home/test/ok2delete/include -DNDEBUG -I. -I../lib -DNDEBUG -O2 -DGIT_COMMIT=\"f8a65c5\" -Wall -c -o io/cli.o io/cli.c
/usr/bin/cc -I/home/test/ok2delete/include -DNDEBUG -I. -I../lib -DNDEBUG -O2 -DGIT_COMMIT=\"f8a65c5\" -Wall -c -o io/output.o io/output.c
...

The strategy is consistent with GNU's coding standards. The good thing about following GNU's advice here is, it actually avoids the compile problems a user experiences due to whacking a user's flags. Also see Section 7.2.3, Variables for Specifying Commands in the GNU manual.

Here is a patch that fixes the build: ecgen.patch.zip.