Skip to contentSkip to conversation

Makefile: Fix build on MSYS2 and Cygwin

PATCH In review Carles Cufi 2 participants 2 messages Apr 28, 2017 – May 10, 2017 1 message not in the archive Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: Fix build on MSYS2 and Cygwin

From: Carles Cufi <[email protected]>

The host compiler on MSYS2 and Cygwin does not allow the -fPIC option, issuing a warning that is treated as an error and stops the build. Detect whether we're running under MSYS2 or Cygwin and avoid adding -fPIC to prevent the error from happening.

---
 Makefile | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
Makefile +8 -3
index beca4a0..62fb9b8 100644
@@ -18,7 +18,7 @@ CONFIG_LOCALVERSION =
 CPPFLAGS = -I libfdt -I .
 WARNINGS = -Wall -Wpointer-arith -Wcast-qual -Wnested-externs \
 	-Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -Wshadow
-CFLAGS = -g -Os -fPIC -Werror $(WARNINGS)
+CFLAGS = -g -Os -Werror $(WARNINGS)
 
 BISON = bison
 LEX = flex
@@ -33,7 +33,12 @@ LIBDIR = $(PREFIX)/lib
 INCLUDEDIR = $(PREFIX)/include
 
 HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \
-	    sed -e 's/\(cygwin\).*/cygwin/')
+		sed -e 's/\(cygwin\|msys\).*/\1/')
+
+ifneq ($(HOSTOS),$(filter $(HOSTOS),msys cygwin))
+CFLAGS += -fPIC
+LDFLAGS += -fPIC
+endif
 
 ifeq ($(HOSTOS),darwin)
 SHAREDLIB_EXT=dylib
@@ -330,7 +335,7 @@ clean: libfdt_clean pylibfdt_clean tests_clean
 
 $(LIBFDT_lib):
 	@$(VECHO) LD $@
-	$(CC) $(LDFLAGS) -fPIC $(SHAREDLIB_LINK_OPTIONS)$(LIBFDT_soname) -o $(LIBFDT_lib) $^
+	$(CC) $(LDFLAGS) $(SHAREDLIB_LINK_OPTIONS)$(LIBFDT_soname) -o $(LIBFDT_lib) $^
 
 %.lex.c: %.l
 	@$(VECHO) LEX $@
Signature · 1 line
2.9.3

A message that is not in the archive (sent off-list or not yet received): 20170428091255.9543-1-carles.cufi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw
Quoted 8 lines · Carles Cufi

From: Carles Cufi <[email protected]>

The host compiler on MSYS2 and Cygwin does not allow the -fPIC option, issuing a warning that is treated as an error and stops the build. Detect whether we're running under MSYS2 or Cygwin and avoid adding -fPIC to prevent the error from happening.

Sorry I didn't reply to this sooner.

I approve the goal of fixing compile, but I'm not entirely happy with the way it's done here. Rather than altering CFLAGS and LDFLAGS on the fly for everything else, I'd prefer to add a new intermediate variable SHAREDLIB_CFLAGS or similar which is set according to HOSTOS< then included into the standard CFLAGS. We already have a SHAREDLIB_LINK_OPTIONS flag that can do this for the link time flags.

Quoted 40 lines
---
 Makefile | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
Makefile +8 -3
index beca4a0..62fb9b8 100644
@@ -18,7 +18,7 @@ CONFIG_LOCALVERSION =
 CPPFLAGS = -I libfdt -I .
 WARNINGS = -Wall -Wpointer-arith -Wcast-qual -Wnested-externs \
 	-Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -Wshadow
-CFLAGS = -g -Os -fPIC -Werror $(WARNINGS)
+CFLAGS = -g -Os -Werror $(WARNINGS)
 
 BISON = bison
 LEX = flex
@@ -33,7 +33,12 @@ LIBDIR = $(PREFIX)/lib
 INCLUDEDIR = $(PREFIX)/include
 
 HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \
-	    sed -e 's/\(cygwin\).*/cygwin/')
+		sed -e 's/\(cygwin\|msys\).*/\1/')
+
+ifneq ($(HOSTOS),$(filter $(HOSTOS),msys cygwin))
+CFLAGS += -fPIC
+LDFLAGS += -fPIC
+endif
 
 ifeq ($(HOSTOS),darwin)
 SHAREDLIB_EXT=dylib
@@ -330,7 +335,7 @@ clean: libfdt_clean pylibfdt_clean tests_clean
 
 $(LIBFDT_lib):
 	@$(VECHO) LD $@
-	$(CC) $(LDFLAGS) -fPIC $(SHAREDLIB_LINK_OPTIONS)$(LIBFDT_soname) -o $(LIBFDT_lib) $^
+	$(CC) $(LDFLAGS) $(SHAREDLIB_LINK_OPTIONS)$(LIBFDT_soname) -o $(LIBFDT_lib) $^
 
 %.lex.c: %.l
 	@$(VECHO) LEX $@
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson