(svn r4149) -Codechange: [OSX] rewrite of how universal binaries are compiled
authorbjarni
Wed, 29 Mar 2006 16:28:41 +0000
changeset 3354 88222b8dafef
parent 3353 2f6e03bd3c29
child 3355 a653b8e47f27
(svn r4149) -Codechange: [OSX] rewrite of how universal binaries are compiled
Now OSX stores object files in .OSX and instead of making FAT object files, there are one for each architecture
Each architecture got their own targets to make a non-FAT binary and in the end, lipo will merge them into one binary

It's now possible to select which architectures you want to support by defining OTTD_PPC, OTTD_PPC970 (G5) and/or OTTD_i386
All combos are supported. UNIVERSAL_BINARY and TRIPLE_BINARY can still be used even though it's possible to gain the same result by using the new flags
Making a universal build when you already got part of it compiled (say the PPC part), it will reuse it and only compile the i386 part to save time
Note: in some cases when you switch flags, you risk that openttd is not updated. Delete it and try again. The Makefile can't solve this except if it forces linking each time

This fixes: FS#87 universal binary building borked in 0.4.7
Now universal binaries work on OSX 10.3.9 again

Building universal binaries no longer needs to store flags in Makefile.config as the new design makes it possible to figure everything out automatically
Makefile
makefiledir/Makefile.config_writer
os/macosx/Makefile
os/macosx/Makefile.setup
--- a/Makefile	Wed Mar 29 06:51:13 2006 +0000
+++ b/Makefile	Wed Mar 29 16:28:41 2006 +0000
@@ -108,6 +108,8 @@
 #
 # UNIVERSAL_BINARY: builds a universal binary for OSX. Make sure you got both PPC and x86 libs. Only works with GCC 4 or newer
 # TRIPLE_BINARY: builds a universal binary with the addition of code optimised for G5 (which means a total of 3 binaries in one file)
+# OTTD_PPC, OTTD_PPC970, OTTD_i386: compile for target architecture.
+#    Multiple flags can be used so OTTD_PPC:=1 OTTD_i386:=1 produces the same result as UNIVERSAL_BINARY
 #
 # JAGUAR: Crosscompiling for OSX 1.2.8 (codenamed Jaguar). Only works if OSX is defined too. Only works with GCC 4 or newer
 #	This can be changed to any PPC version of OSX by changing the ppc flags in Makefile.config
@@ -130,7 +132,7 @@
 
 # Makefile version tag
 # it checks if the version tag in Makefile.config is the same and force update outdated config files
-MAKEFILE_VERSION:=9
+MAKEFILE_VERSION:=10
 
 # CONFIG_WRITER has to be found even for manual configuration
 CONFIG_WRITER=makefiledir/Makefile.config_writer
@@ -345,11 +347,6 @@
 ifdef OSX
 # these compilerflags makes the app run as fast as possible without making the app unstable. It works on G3 or newer
 BASECFLAGS += -O3 -funroll-loops -fsched-interblock -falign-loops=16 -falign-jumps=16 -falign-functions=16 -falign-jumps-max-skip=15 -falign-loops-max-skip=15 -mdynamic-no-pic
-ifdef IS_G5
-ifndef UNIVERSAL_BINARY
-BASECFLAGS += $(G5_FLAGS)
-endif
-endif
 else
 ifdef MORPHOS
 BASECFLAGS += -I/gg/os-include -O2 -noixemul -fstrict-aliasing -fexpensive-optimizations
@@ -830,9 +827,12 @@
 	$(Q)$(CC_HOST) $(CFLAGS_HOST) $(CDEFS) $< -o $@
 
 
+ifndef NATIVE_OSX
+# OSX links in os/macosx/Makefile to handle universal binaries better
 $(TTD): $(OBJS) $(MAKE_CONFIG)
 	@echo '===> Linking $@'
 	$(Q)$(CC) $(LDFLAGS) $(TTDLDFLAGS) $(OBJS) $(LIBS) -o $@
+endif
 
 $(STRGEN): strgen/strgen.c endian_host.h
 	@echo '===> Compiling and Linking $@'
@@ -899,7 +899,7 @@
 clean:
 	@echo '===> Cleaning up'
 # endian.h is out-dated and no longer in use, so it can be removed soon
-	$(Q)rm -rf .deps *~ $(TTD) $(STRGEN) core table/strings.h $(LANGS) $(OBJS) $(OSX_MIDI_PLAYER_FILE) endian.h endian_host.h endian_target.h $(ENDIAN_CHECK)
+	$(Q)rm -rf .deps *~ $(TTD) $(STRGEN) core table/strings.h $(LANGS) $(OBJS) $(OSX_MIDI_PLAYER_FILE) endian.h endian_host.h endian_target.h $(ENDIAN_CHECK) .OSX
 
 mrproper: clean
 	$(Q)rm -rf $(MAKE_CONFIG)
@@ -1006,20 +1006,19 @@
 	$(Q)$(CC) $(OBJCFLAGS) $(CDEFS) -MM $< | sed 's#^$(@F:%.d=%.o):#$@ $(@:.deps/%.d=%.o):#' > $@
 
 
-ifndef TRIPLE_BINARY
-# building tripple binary object files is handled in os/macosx/Makefile
-# TARGET_CPU_FLAGS is used to set target CPUs in OSX universal binaries. It's empty for all other builds
+ifndef NATIVE_OSX
+# OSX uses os/macosx/Makefile to compile files
 %.o: %.c $(MAKE_CONFIG)
 	@echo '===> Compiling $<'
-	$(Q)$(CC) $(TARGET_CPU_FLAGS) $(CFLAGS) $(CDEFS) -c -o $@ $<
+	$(Q)$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $<
 
 %.o: %.cpp  $(MAKE_CONFIG)
 	@echo '===> Compiling $<'
-	$(Q)$(CXX) $(TARGET_CPU_FLAGS) $(CFLAGS) $(CDEFS) -c -o $@ $<
+	$(Q)$(CXX) $(CFLAGS) $(CDEFS) -c -o $@ $<
 
 %.o: %.m  $(MAKE_CONFIG)
 	@echo '===> Compiling $<'
-	$(Q)$(CC) $(TARGET_CPU_FLAGS) $(CFLAGS) $(CDEFS) -c -o $@ $<
+	$(Q)$(CC) $(CFLAGS) $(CDEFS) -c -o $@ $<
 endif
 
 %.o: %.rc
--- a/makefiledir/Makefile.config_writer	Wed Mar 29 06:51:13 2006 +0000
+++ b/makefiledir/Makefile.config_writer	Wed Mar 29 16:28:41 2006 +0000
@@ -83,17 +83,6 @@
 	$(call CONFIG_LINE,MINGW:=$(MINGW))
 	$(call CONFIG_LINE,)
 
-	$(call CONFIG_LINE,\# Universal binary setup)
-	$(call CONFIG_LINE,\# use these settings for building universal binaries. Most systems should work with the default settings)
-	$(call CONFIG_LINE,CFLAGS_UNIVERSAL:=$(CFLAGS_UNIVERSAL))
-	$(call CONFIG_LINE,LDFLAGS_UNIVERSAL:=$(LDFLAGS_UNIVERSAL))
-
-	$(call CONFIG_LINE,CFLAGS_JAGUAR:=$(CFLAGS_JAGUAR))
-	$(call CONFIG_LINE,LDFLAGS_JAGUAR:=$(LDFLAGS_JAGUAR))
-	$(call CONFIG_LINE,)
-	$(call CONFIG_LINE,G5_FLAGS:=$(G5_FLAGS))
-	$(call CONFIG_LINE,)
-
 	$(call CONFIG_LINE,\# For cross-compiling)
 	$(call CONFIG_LINE,CC_TARGET:=$(CC_TARGET))
 	$(call CONFIG_LINE,CC_HOST:=$(CC_HOST))
--- a/os/macosx/Makefile	Wed Mar 29 06:51:13 2006 +0000
+++ b/os/macosx/Makefile	Wed Mar 29 16:28:41 2006 +0000
@@ -2,29 +2,79 @@
 # This makefile is not a standalone makefile, but is called from the general one
 # it contains targets specific to MacOS X
 
-ifdef TRIPLE_BINARY
-# this is to add G5_FLAGS to ppc970 builds only. If the ability to add flags to a single arch only shows up in the future
-# we can get rid of this. Xcode supports arch dependant flags, but we can't do it in the makefile (yet?)
-%.o: %.c $(MAKE_CONFIG)
-	@echo '===> Compiling $<'
-	$(Q)$(CC) $(CFLAGS) $(CDEFS) -arch ppc -c -arch i386 -o $@.uni $<
-	$(Q)$(CC) $(CFLAGS) $(CDEFS) $(G5_FLAGS) -arch ppc970 -c -o $@.ppc970 $<
-	$(Q)lipo -create -output $@ $@.uni $@.ppc970
-	$(Q)rm $@.uni $@.ppc970
+ifdef NATIVE_OSX
 
-%.o: %.cpp  $(MAKE_CONFIG)
-	@echo '===> Compiling $<'
-	$(Q)$(CXX) $(CFLAGS) $(CDEFS) -arch ppc -arch i386 -c -o $@.uni $<
-	$(Q)$(CXX) $(CFLAGS) $(CDEFS) $(G5_FLAGS) -arch ppc970 -c -o $@.ppc970 $<
-	$(Q)lipo -create -output $@ $@.uni $@.ppc970
-	$(Q)rm $@.uni $@.ppc970
+DEPS_MAGIC := $(shell mkdir -p $(sort $(dir $(OBJS:%.o=.OSX/%))))
 
-%.o: %.m  $(MAKE_CONFIG)
-	@echo '===> Compiling $<'
-	$(Q)$(CC) $(CFLAGS) $(CDEFS) -arch ppc -arch i386 -c -o $@.uni $<
-	$(Q)$(CC) $(CFLAGS) $(CDEFS) -arch ppc970 $(G5_FLAGS) -c -o $@.ppc970 $<
-	$(Q)lipo -create -output $@ $@.uni $@.ppc970
-	$(Q)rm $@.uni $@.ppc970
+ifdef OTTD_PPC
+OTTD_PPC    :=.OSX/openttd.ppc
+OBJS_ppc    := $(OBJS:%.o=.OSX/%.o.ppc)
+endif
+
+ifdef OTTD_i386
+OTTD_i386   :=.OSX/openttd.i386
+OBJS_i386   := $(OBJS:%.o=.OSX/%.o.i386)
+endif
+
+ifdef OTTD_PPC970
+OTTD_PPC970 :=.OSX/openttd.ppc970
+OBJS_ppc970 := $(OBJS:%.o=.OSX/%.o.ppc970)
+endif
+
+# targets to link OpenTTD
+$(TTD): $(OTTD_PPC) $(OTTD_i386) $(OTTD_PPC970)
+	@echo '===> Linking $@ into a single file'
+	$(Q)lipo -create -output $@ $(OTTD_PPC) $(OTTD_i386) $(OTTD_PPC970)
+
+$(OTTD_PPC): $(MAKE_CONFIG) $(OBJS_ppc)
+	@echo '[PowerPC] Linking $(TTD)'
+	$(Q)$(CC) $(LDFLAGS) $(TTDLDFLAGS) $(OBJS_ppc) $(LIBS) $(LDFLAGS_PPC) -arch ppc -o $@
+
+$(OTTD_i386): $(MAKE_CONFIG) $(OBJS_i386)
+	@echo '[i386] Linking $(TTD)'
+	$(Q)$(CC) $(LDFLAGS) $(TTDLDFLAGS) $(OBJS_i386) $(LIBS) $(LDFLAGS_i386) -arch i386 -o $@
+
+$(OTTD_PPC970): $(MAKE_CONFIG) $(OBJS_ppc970)
+	@echo '[PowerPC G5] Linking $(TTD)'
+	$(Q)$(CC) $(LDFLAGS) $(TTDLDFLAGS) $(OBJS_ppc970) $(LIBS) $(LDFLAGS_PPC) $(G5_FLAGS) -arch ppc970 -o $@
+
+# targets to compile each c, m and cpp file
+.OSX/%.o.ppc: %.c      .deps/%.d
+	@echo '[PowerPC] Compiling $<'
+	$(Q)$(CC) $(CFLAGS) $(CFLAGS_PPC) $(CDEFS) -arch ppc -c -o $@ $<
+
+.OSX/%.o.ppc: %.cpp    .deps/%.d
+	@echo '[PowerPC] Compiling $<'
+	$(Q)$(CPP) $(CFLAGS) $(CFLAGS_PPC) $(CDEFS) -arch ppc -c -o $@ $<
+
+.OSX/%.o.ppc: %.m      .deps/%.d
+	@echo '[PowerPC] Compiling $<'
+	$(Q)$(CC) $(CFLAGS) $(CFLAGS_PPC) $(CDEFS) -arch ppc -c -o $@ $<
+
+.OSX/%.o.i386: %.c     .deps/%.d
+	@echo '[i386] Compiling $<'
+	$(Q)$(CC) $(CFLAGS) $(CFLAGS_i386) $(CDEFS) -arch i386 -c -o $@ $<
+
+.OSX/%.o.i386: %.cpp   .deps/%.d
+	@echo '[i386] Compiling $<'
+	$(Q)$(CPP) $(CFLAGS) $(CFLAGS_i386) $(CDEFS) -arch i386 -c -o $@ $<
+
+.OSX/%.o.i386: %.m     .deps/%.d
+	@echo '[i386] Compiling $<'
+	$(Q)$(CC) $(CFLAGS) $(CFLAGS_i386) $(CDEFS) -arch i386 -c -o $@ $<
+
+.OSX/%.o.ppc970: %.c   .deps/%.d
+	@echo '[PowerPC G5] Compiling $<'
+	$(Q)$(CC) $(CFLAGS) $(CFLAGS_PPC) $(G5_FLAGS) $(CDEFS) -arch ppc970 -c -o $@ $<
+
+.OSX/%.o.ppc970: %.cpp .deps/%.d
+	@echo '[PowerPC G5] Compiling $<'
+	$(Q)$(CPP) $(CFLAGS) $(CFLAGS_PPC) $(G5_FLAGS) $(CDEFS) -arch ppc970 -c -o $@ $<
+
+.OSX/%.o.ppc970: %.m   .deps/%.d
+	@echo '[PowerPC G5] Compiling $<'
+	$(Q)$(CC) $(CFLAGS) $(CFLAGS_PPC) $(G5_FLAGS) $(CDEFS) -arch ppc970 -c -o $@ $<
+
 endif
 
 ifdef JAGUAR
--- a/os/macosx/Makefile.setup	Wed Mar 29 06:51:13 2006 +0000
+++ b/os/macosx/Makefile.setup	Wed Mar 29 16:28:41 2006 +0000
@@ -18,7 +18,7 @@
 
 ifdef TRIPLE_BINARY
 ifdef DEBUG
-$(error no G5 optimisation is made in debug builds, so tripple binaries aren't possible. Use UNIVERSAL_BINARY instead if you really want a universal debug build)
+$(error no G5 optimisation is made in debug builds, so triple binaries aren't possible. Use UNIVERSAL_BINARY instead if you really want a universal debug build)
 endif
 UNIVERSAL_BINARY:=1
 endif
@@ -36,24 +36,16 @@
 endif
 endif
 
-# setup flags if none are defined
-ifndef CFLAGS_JAGUAR
-	CFLAGS_JAGUAR:= -isysroot /Developer/SDKs/MacOSX10.2.8.sdk
-endif
-ifndef LDFLAGS_JAGUAR
-	LDFLAGS_JAGUAR:= -Wl,-syslibroot,/Developer/SDKs/MacOSX10.2.8.sdk
+ifeq ($(shell uname), Darwin)
+	# it's a hardware mac, not crosscompiling
+	NATIVE_OSX:=1
 endif
 
-ifndef CFLAGS_UNIVERSAL
-	CFLAGS_UNIVERSAL:= -isysroot /Developer/SDKs/MacOSX10.4u.sdk
+ifndef PPC_OSX_TARGET
+PPC_OSX_TARGET:=10.3.9
 endif
-ifndef LDFLAGS_UNIVERSAL
-	LDFLAGS_UNIVERSAL:= -Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk
-endif
-
-ifdef JAGUAR
-	CFLAGS:= $(CFLAGS_JAGUAR) -arch ppc
-	LDFLAGS:= $(LDFLAGS_JAGUAR)
+ifndef i386_OSX_TARGET
+i386_OSX_TARGET:=10.4u
 endif
 
 ifndef G5_FLAGS
@@ -61,26 +53,70 @@
 endif
 
 ifdef UNIVERSAL_BINARY
-TARGET_CPU_FLAGS:= -arch ppc -arch i386
-LDFLAGS := $(LDFLAGS_UNIVERSAL) -arch ppc -arch i386
-CFLAGS += $(CFLAGS_UNIVERSAL)
-ifdef TRIPLE_BINARY
-LDFLAGS += -arch ppc970
+	OTTD_PPC:=1
+	OTTD_i386:=1
+	ifdef TRIPLE_BINARY
+		OTTD_PPC970:=1
+	endif
 endif
+
+# if any targets have been defined by now, we are crosscompiling and we will set up paths accordingly
+ifdef OTTD_PPC
+	CFLAGS_PPC   += -isysroot /Developer/SDKs/MacOSX$(PPC_OSX_TARGET).sdk
+	LDFLAGS_PPC  += -Wl,-syslibroot,/Developer/SDKs/MacOSX$(PPC_OSX_TARGET).sdk
+endif
+
+ifdef OTTD_i386
+	CFLAGS_i386  += -isysroot /Developer/SDKs/MacOSX$(i386_OSX_TARGET).sdk
+	LDFLAGS_i386 += -Wl,-syslibroot,/Developer/SDKs/MacOSX$(i386_OSX_TARGET).sdk
+endif
+
+ifdef OTTD_PPC970
+	ifndef OTTD_PPC
+		CFLAGS_PPC   += -isysroot /Developer/SDKs/MacOSX$(PPC_OSX_TARGET).sdk
+		LDFLAGS_PPC  += -Wl,-syslibroot,/Developer/SDKs/MacOSX$(PPC_OSX_TARGET).sdk
+	endif
+endif
+
+ifdef JAGUAR
+	CFLAGS  += -isysroot /Developer/SDKs/MacOSX10.2.8.sdk
+	LDFLAGS += -Wl,-syslibroot,/Developer/SDKs/MacOSX10.2.8.sdk
+	OTTD_PPC:=1
 endif
 
 # setting up flags to make a binary, that fits the system it builds on
-ifndef UNIVERSAL_BINARY
-	ifndef JAGUAR
-		ifeq ($(shell uname), Darwin)
-			# it's a hardware mac, not crosscompiling
+ifdef NATIVE_OSX
+	ifndef UNIVERSAL_BINARY
+		# we are not crosscompiling for other macs
+		ifndef JAGUAR
 			# the next line fails if it got whitespace in front of it
 $(shell $(CC) os/macosx/G5_detector.c -o os/macosx/G5_detector)
 			IS_G5:=$(shell os/macosx/G5_detector)
 			ifeq ($(shell uname -r), 6.8)
 			# OSX 10.2.8 uses Darwin 6.8, so we better set JAGUAR so we avoid the stuff that was added in 10.3 or later
 				JAGUAR:=1
+				OTTD_PPC:=1
 			endif
 		endif
 	endif
 endif
+
+ifdef NATIVE_OSX
+	ifndef OTTD_PPC
+		ifndef OTTD_i386
+			ifndef OTTD_PPC970
+				# no flags have been set for target versions of OSX, so we will set it to compile for the current host
+				ifeq ($(shell uname -p), powerpc)
+					ifdef IS_G5
+						OTTD_PPC970:=1
+					else
+						OTTD_PPC:=1
+					endif
+				else
+					# we are not using a PowerPC CPU, so we assume that it's an Intel mac
+					OTTD_i386:=1
+				endif
+			endif
+		endif
+	endif
+endif
\ No newline at end of file