1 #
   2 # Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
   3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 #
   5 # This code is free software; you can redistribute it and/or modify it
   6 # under the terms of the GNU General Public License version 2 only, as
   7 # published by the Free Software Foundation.  Oracle designates this
   8 # particular file as subject to the "Classpath" exception as provided
   9 # by Oracle in the LICENSE file that accompanied this code.
  10 #
  11 # This code is distributed in the hope that it will be useful, but WITHOUT
  12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 # version 2 for more details (a copy is included in the LICENSE file that
  15 # accompanied this code).
  16 #
  17 # You should have received a copy of the GNU General Public License version
  18 # 2 along with this work; if not, write to the Free Software Foundation,
  19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 #
  21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 # or visit www.oracle.com if you need additional information or have any
  23 # questions.
  24 #
  25 
  26 ##########################################################################################
  27 #
  28 # Workhorse makefile for creating ONE cross compiler
  29 # Needs either to be from BUILD -> BUILD OR have
  30 # BUILD -> HOST prebuilt
  31 #
  32 # NOTE: There is a bug here. We don't limit the
  33 # PATH when building BUILD -> BUILD, which means that
  34 # if you configure after you've once build the BUILD->BUILD
  35 # compiler THAT one will be picked up as the compiler for itself.
  36 # This is not so great, especially if you did a partial delete
  37 # of the target tree.
  38 #
  39 # Fix this...
  40 #
  41 
  42 $(info TARGET=$(TARGET))
  43 $(info HOST=$(HOST))
  44 $(info BUILD=$(BUILD))
  45 
  46 ARCH := $(word 1,$(subst -, ,$(TARGET)))
  47 
  48 ##########################################################################################
  49 # Define external dependencies
  50 
  51 # Latest that could be made to work.
  52 gcc_ver := gcc-4.9.2
  53 binutils_ver := binutils-2.25
  54 ccache_ver := ccache-3.2.1
  55 mpfr_ver := mpfr-3.0.1
  56 gmp_ver := gmp-4.3.2
  57 mpc_ver := mpc-1.0.1
  58 
  59 GCC := http://ftp.gnu.org/pub/gnu/gcc/$(gcc_ver)/$(gcc_ver).tar.bz2
  60 BINUTILS := http://ftp.gnu.org/pub/gnu/binutils/$(binutils_ver).tar.bz2
  61 CCACHE := http://samba.org/ftp/ccache/$(ccache_ver).tar.gz
  62 MPFR := http://www.mpfr.org/${mpfr_ver}/${mpfr_ver}.tar.bz2
  63 GMP := http://ftp.gnu.org/pub/gnu/gmp/${gmp_ver}.tar.bz2
  64 MPC := http://www.multiprecision.org/mpc/download/${mpc_ver}.tar.gz
  65 
  66 # RPMs in OEL6.4
  67 LINUX_VERSION := OEL6.4
  68 RPM_LIST := \
  69     kernel-headers \
  70     glibc glibc-headers glibc-devel \
  71     cups-libs cups-devel \
  72     libX11 libX11-devel \
  73     xorg-x11-proto-devel \
  74     alsa-lib alsa-lib-devel \
  75     libXext libXext-devel \
  76     libXtst libXtst-devel \
  77     libXrender libXrender-devel \
  78     freetype freetype-devel \
  79     libXt libXt-devel \
  80     libSM libSM-devel \
  81     libICE libICE-devel \
  82     libXi libXi-devel \
  83     libXdmcp libXdmcp-devel \
  84     libXau libXau-devel \
  85     libgcc \
  86     zlib zlib-devel \
  87     libffi libffi-devel \
  88     fontconfig fontconfig-devel
  89 
  90 ifeq ($(ARCH),x86_64)
  91   RPM_DIR ?= $(RPM_DIR_x86_64)
  92   RPM_ARCHS := x86_64 noarch
  93   ifeq ($(BUILD),$(HOST))
  94     ifeq ($(TARGET),$(HOST))
  95       # When building the native compiler for x86_64, enable mixed mode.
  96       RPM_ARCHS += i386 i686
  97     endif
  98   endif
  99 else
 100   RPM_DIR ?= $(RPM_DIR_i686)
 101   RPM_ARCHS := i386 i686
 102 endif
 103 
 104 # Sort to remove duplicates
 105 RPM_FILE_LIST := $(sort $(foreach a,$(RPM_ARCHS),$(wildcard $(patsubst %,$(RPM_DIR)/%*$a.rpm,$(RPM_LIST)))))
 106 
 107 #$(info RPM_FILE_LIST $(RPM_FILE_LIST))
 108 
 109 ifeq ($(RPM_FILE_LIST),)
 110   $(error Found no RPMs, RPM_DIR must point to list of directories to search for RPMs)
 111 endif
 112 
 113 ##########################################################################################
 114 # Define common directories and files
 115 
 116 # Ensure we have 32-bit libs also for x64. We enable mixed-mode.
 117 ifeq (x86_64,$(ARCH))
 118   LIBDIRS := lib64 lib
 119   CFLAGS_lib := -m32
 120 else
 121   LIBDIRS := lib
 122 endif
 123 
 124 # Define directories
 125 RESULT := $(OUTPUTDIR)/result
 126 BUILDDIR := $(OUTPUTDIR)/$(HOST)/$(TARGET)
 127 PREFIX := $(RESULT)/$(HOST)
 128 TARGETDIR := $(PREFIX)/$(TARGET)
 129 SYSROOT := $(TARGETDIR)/sysroot
 130 DOWNLOAD := $(OUTPUTDIR)/download
 131 SRCDIR := $(OUTPUTDIR)/src
 132 
 133 # Marker file for unpacking rpms
 134 rpms := $(SYSROOT)/rpms_unpacked
 135 
 136 # Need to patch libs that are linker scripts to use non-absolute paths
 137 libs := $(SYSROOT)/libs_patched
 138 
 139 ##########################################################################################
 140 # Unpack source packages
 141 
 142 # Generate downloading + unpacking of sources.
 143 define Download
 144   $(1)_DIR = $(abspath $(SRCDIR)/$(basename $(basename $(notdir $($(1))))))
 145   $(1)_CFG = $$($(1)_DIR)/configure
 146   $(1)_FILE = $(DOWNLOAD)/$(notdir $($(1)))
 147 
 148   $$($(1)_CFG) : $$($(1)_FILE)
 149         mkdir -p $$(SRCDIR)
 150         tar -C $$(SRCDIR) -x$$(if $$(findstring .gz, $$<),z,j)f $$<
 151         $$(foreach p,$$(abspath $$(wildcard $$(notdir $$($(1)_DIR)).patch)), \
 152           echo PATCHING $$(p) ; \
 153           patch -d $$($(1)_DIR) -p1 -i $$(p) ; \
 154         )
 155         touch $$@
 156 
 157   $$($(1)_FILE) :
 158         wget -P $(DOWNLOAD) $$($(1))
 159 endef
 160 
 161 # Download and unpack all source packages
 162 $(foreach p,GCC BINUTILS CCACHE MPFR GMP MPC,$(eval $(call Download,$(p))))
 163 
 164 ##########################################################################################
 165 # Unpack RPMS
 166 
 167 # Note. For building linux you should install rpm2cpio.
 168 define unrpm
 169   $(SYSROOT)/$(notdir $(1)).unpacked \
 170     : $(1)
 171   $$(rpms) : $(SYSROOT)/$(notdir $(1)).unpacked
 172 endef
 173 
 174 %.unpacked :
 175         $(info Unpacking target rpms and libraries from $<)
 176         @(mkdir -p $(@D); \
 177         cd $(@D); \
 178         rpm2cpio $< | \
 179             cpio --extract --make-directories \
 180                 -f \
 181                 "./usr/share/doc/*" \
 182                 "./usr/share/man/*" \
 183                 "./usr/X11R6/man/*" \
 184                 "*/X11/locale/*" \
 185             || die ; )
 186         touch $@
 187 
 188 $(foreach p,$(RPM_FILE_LIST),$(eval $(call unrpm,$(p))))
 189 
 190 ##########################################################################################
 191 
 192 # Note: MUST create a <sysroot>/usr/lib even if not really needed.
 193 # gcc will use a path relative to it to resolve lib64. (x86_64).
 194 # we're creating multi-lib compiler with 32bit libc as well, so we should
 195 # have it anyway, but just to make sure...
 196 # Patch libc.so and libpthread.so to force linking against libraries in sysroot
 197 # and not the ones installed on the build machine.
 198 $(libs) : $(rpms)
 199         @echo Patching libc and pthreads
 200         @(for f in `find $(SYSROOT) -name libc.so -o -name libpthread.so`; do \
 201           (cat $$f | sed -e 's|/usr/lib64/||g' \
 202               -e 's|/usr/lib/||g' \
 203               -e 's|/lib64/||g' \
 204               -e 's|/lib/||g' ) > $$f.tmp ; \
 205           mv $$f.tmp $$f ; \
 206         done)
 207         @mkdir -p $(SYSROOT)/usr/lib
 208         @touch $@
 209 
 210 ##########################################################################################
 211 # Create links for ffi header files so that they become visible by default when using the
 212 # devkit.
 213 
 214 $(SYSROOT)/usr/include/ffi.h: $(rpms)
 215         cd $(@D) && rm $(@F) && ln -s ../lib/libffi-*/include/$(@F) .
 216 
 217 $(SYSROOT)/usr/include/ffitarget.h: $(rpms)
 218         cd $(@D) && rm $(@F) && ln -s ../lib/libffi-*/include/$(@F) .
 219 
 220 SYSROOT_LINKS += $(SYSROOT)/usr/include/ffi.h $(SYSROOT)/usr/include/ffitarget.h
 221 
 222 ##########################################################################################
 223 
 224 # Define marker files for each source package to be compiled
 225 $(foreach t,binutils mpfr gmp mpc gcc ccache,$(eval $(t) = $(TARGETDIR)/$($(t)_ver).done))
 226 
 227 ##########################################################################################
 228 
 229 # Default base config
 230 CONFIG = --target=$(TARGET) \
 231     --host=$(HOST) --build=$(BUILD) \
 232     --prefix=$(PREFIX)
 233 
 234 PATHEXT = $(RESULT)/$(BUILD)/bin:
 235 
 236 PATHPRE = PATH=$(PATHEXT)$(PATH)
 237 BUILDPAR = -j16
 238 
 239 # Default commands to when making
 240 MAKECMD =
 241 INSTALLCMD = install
 242 
 243 
 244 declare_tools = CC$(1)=$(2)gcc LD$(1)=$(2)ld AR$(1)=$(2)ar AS$(1)=$(2)as RANLIB$(1)=$(2)ranlib CXX$(1)=$(2)g++ OBJDUMP$(1)=$(2)objdump
 245 
 246 ifeq ($(HOST),$(BUILD))
 247   ifeq ($(HOST),$(TARGET))
 248     TOOLS = $(call declare_tools,_FOR_TARGET,)
 249   endif
 250 endif
 251 
 252 TOOLS ?= $(call declare_tools,_FOR_TARGET,$(TARGET)-)
 253 
 254 ##########################################################################################
 255 
 256 # Create a TARGET bfd + libiberty only.
 257 # Configure one or two times depending on mulitlib arch.
 258 # If multilib, the second should be 32-bit, and we resolve
 259 # CFLAG_<name> to most likely -m32.
 260 define mk_bfd
 261   $$(info Libs for $(1))
 262   $$(BUILDDIR)/$$(binutils_ver)-$(subst /,-,$(1))/Makefile \
 263       : CFLAGS += $$(CFLAGS_$(1))
 264   $$(BUILDDIR)/$$(binutils_ver)-$(subst /,-,$(1))/Makefile \
 265       : LIBDIRS = --libdir=$(TARGETDIR)/$(1)
 266 
 267   bfdlib += $$(TARGETDIR)/$$(binutils_ver)-$(subst /,-,$(1)).done
 268   bfdmakes += $$(BUILDDIR)/$$(binutils_ver)-$(subst /,-,$(1))/Makefile
 269 endef
 270 
 271 # Create one set of bfds etc for each multilib arch
 272 $(foreach l,$(LIBDIRS),$(eval $(call mk_bfd,$(l))))
 273 
 274 # Only build these two libs.
 275 $(bfdlib) : MAKECMD = all-libiberty all-bfd
 276 $(bfdlib) : INSTALLCMD = install-libiberty install-bfd
 277 
 278 # Building targets libbfd + libiberty. HOST==TARGET, i.e not
 279 # for a cross env.
 280 $(bfdmakes) : CONFIG = --target=$(TARGET) \
 281     --host=$(TARGET) --build=$(BUILD) \
 282     --prefix=$(TARGETDIR) \
 283     --with-sysroot=$(SYSROOT) \
 284     $(LIBDIRS)
 285 
 286 $(bfdmakes) : TOOLS = $(call declare_tools,_FOR_TARGET,$(TARGET)-) $(call declare_tools,,$(TARGET)-)
 287 
 288 ##########################################################################################
 289 
 290 $(gcc) \
 291     $(binutils) \
 292     $(gmp) \
 293     $(mpfr) \
 294     $(mpc) \
 295     $(bfdmakes) \
 296     $(ccache) : ENVS += $(TOOLS)
 297 
 298 # libdir to work around hateful bfd stuff installing into wrong dirs...
 299 # ensure we have 64 bit bfd support in the HOST library. I.e our
 300 # compiler on i686 will know 64 bit symbols, BUT later
 301 # we build just the libs again for TARGET, then with whatever the arch
 302 # wants.
 303 $(BUILDDIR)/$(binutils_ver)/Makefile : CONFIG += --enable-64-bit-bfd --libdir=$(PREFIX)/$(word 1,$(LIBDIRS))
 304 
 305 # Makefile creation. Simply run configure in build dir.
 306 $(bfdmakes) \
 307 $(BUILDDIR)/$(binutils_ver)/Makefile \
 308     : $(BINUTILS_CFG)
 309         $(info Configuring $@. Log in $(@D)/log.config)
 310         @mkdir -p $(@D)
 311         ( \
 312           cd $(@D) ; \
 313           $(PATHPRE) $(ENVS) CFLAGS="$(CFLAGS)" \
 314               $(BINUTILS_CFG) \
 315               $(CONFIG) \
 316               --with-sysroot=$(SYSROOT) \
 317               --disable-nls \
 318               --program-prefix=$(TARGET)- \
 319               --enable-multilib \
 320         ) > $(@D)/log.config 2>&1
 321         @echo 'done'
 322 
 323 $(BUILDDIR)/$(mpfr_ver)/Makefile \
 324     : $(MPFR_CFG)
 325         $(info Configuring $@. Log in $(@D)/log.config)
 326         @mkdir -p $(@D)
 327         ( \
 328           cd $(@D) ; \
 329           $(PATHPRE) $(ENVS) CFLAGS="$(CFLAGS)" \
 330               $(MPFR_CFG) \
 331               $(CONFIG) \
 332               --program-prefix=$(TARGET)- \
 333               --enable-shared=no \
 334               --with-gmp=$(PREFIX) \
 335         ) > $(@D)/log.config 2>&1
 336         @echo 'done'
 337 
 338 $(BUILDDIR)/$(gmp_ver)/Makefile \
 339     : $(GMP_CFG)
 340         $(info Configuring $@. Log in $(@D)/log.config)
 341         @mkdir -p $(@D)
 342         ( \
 343           cd $(@D) ; \
 344           $(PATHPRE) $(ENVS) CFLAGS="$(CFLAGS)" \
 345               $(GMP_CFG) \
 346               --host=$(HOST) --build=$(BUILD) \
 347               --prefix=$(PREFIX) \
 348               --disable-nls \
 349               --program-prefix=$(TARGET)- \
 350               --enable-shared=no \
 351               --with-mpfr=$(PREFIX) \
 352         ) > $(@D)/log.config 2>&1
 353         @echo 'done'
 354 
 355 $(BUILDDIR)/$(mpc_ver)/Makefile \
 356     : $(MPC_CFG)
 357         $(info Configuring $@. Log in $(@D)/log.config)
 358         @mkdir -p $(@D)
 359         ( \
 360           cd $(@D) ; \
 361           $(PATHPRE) $(ENVS) CFLAGS="$(CFLAGS)" \
 362               $(MPC_CFG) \
 363               $(CONFIG) \
 364               --program-prefix=$(TARGET)- \
 365               --enable-shared=no \
 366               --with-mpfr=$(PREFIX) \
 367               --with-gmp=$(PREFIX) \
 368         ) > $(@D)/log.config 2>&1
 369         @echo 'done'
 370 
 371 # Only valid if glibc target -> linux
 372 # proper destructor handling for c++
 373 ifneq (,$(findstring linux,$(TARGET)))
 374   $(BUILDDIR)/$(gcc_ver)/Makefile : CONFIG += --enable-__cxa_atexit
 375 endif
 376 
 377 # Want:
 378 # c,c++
 379 # shared libs
 380 # multilib (-m32/-m64 on x64)
 381 # skip native language.
 382 # and link and assemble with the binutils we created
 383 # earlier, so --with-gnu*
 384 $(BUILDDIR)/$(gcc_ver)/Makefile \
 385     : $(GCC_CFG)
 386         $(info Configuring $@. Log in $(@D)/log.config)
 387         mkdir -p $(@D)
 388         ( \
 389           cd $(@D) ; \
 390           $(PATHPRE) $(ENVS) $(GCC_CFG) $(EXTRA_CFLAGS) \
 391               $(CONFIG) \
 392               --with-sysroot=$(SYSROOT) \
 393               --enable-languages=c,c++ \
 394               --enable-shared \
 395               --enable-multilib \
 396               --disable-nls \
 397               --with-gnu-as \
 398               --with-gnu-ld \
 399               --with-mpfr=$(PREFIX) \
 400               --with-gmp=$(PREFIX) \
 401               --with-mpc=$(PREFIX) \
 402         ) > $(@D)/log.config 2>&1
 403         @echo 'done'
 404 
 405 # need binutils for gcc
 406 $(gcc) : $(binutils)
 407 
 408 # as of 4.3 or so need these for doing config
 409 $(BUILDDIR)/$(gcc_ver)/Makefile : $(gmp) $(mpfr) $(mpc)
 410 $(mpfr) : $(gmp)
 411 $(mpc) : $(gmp) $(mpfr)
 412 
 413 ##########################################################################################
 414 # very straightforward. just build a ccache. it is only for host.
 415 $(BUILDDIR)/$(ccache_ver)/Makefile \
 416     : $(CCACHE_CFG)
 417         $(info Configuring $@. Log in $(@D)/log.config)
 418         @mkdir -p $(@D)
 419         @( \
 420           cd $(@D) ; \
 421           $(PATHPRE) $(ENVS) $(CCACHE_CFG) \
 422               $(CONFIG) \
 423         ) > $(@D)/log.config 2>&1
 424         @echo 'done'
 425 
 426 gccpatch = $(TARGETDIR)/gcc-patched
 427 
 428 ##########################################################################################
 429 # For some reason cpp is not created as a target-compiler
 430 ifeq ($(HOST),$(TARGET))
 431   $(gccpatch) : $(gcc) link_libs
 432         @echo -n 'Creating compiler symlinks...'
 433         @for f in cpp; do \
 434           if [ ! -e $(PREFIX)/bin/$(TARGET)-$$f ]; \
 435           then \
 436             cd $(PREFIX)/bin && \
 437             ln -s $$f $(TARGET)-$$f ; \
 438           fi \
 439         done
 440         @touch $@
 441         @echo 'done'
 442 
 443   ##########################################################################################
 444   # Ugly at best. Seems that when we compile host->host compiler, that are NOT
 445   # the BUILD compiler, the result will not try searching for libs in package root.
 446   # "Solve" this by create links from the target libdirs to where they are.
 447   link_libs:
 448         @echo -n 'Creating library symlinks...'
 449         @$(foreach l,$(LIBDIRS), \
 450         for f in `cd $(PREFIX)/$(l) && ls`; do \
 451           if [ ! -e $(TARGETDIR)/$(l)/$$f ]; then \
 452             mkdir -p $(TARGETDIR)/$(l) && \
 453             cd $(TARGETDIR)/$(l)/ && \
 454             ln -s $(if $(findstring /,$(l)),../,)../../$(l)/$$f $$f; \
 455           fi \
 456         done;)
 457         @echo 'done'
 458 else
 459   $(gccpatch) :
 460         @echo 'done'
 461 endif
 462 
 463 ##########################################################################################
 464 # Build in two steps.
 465 # make <default>
 466 # make install.
 467 # Use path to our build hosts cross tools
 468 # Always need to build cross tools for build host self.
 469 $(TARGETDIR)/%.done : $(BUILDDIR)/%/Makefile
 470         $(info Building $(basename $@). Log in $(<D)/log.build)
 471         $(PATHPRE) $(ENVS) $(MAKE) $(BUILDPAR) -f $< -C $(<D) $(MAKECMD) $(MAKECMD.$(notdir $@)) > $(<D)/log.build 2>&1
 472         @echo -n 'installing...'
 473         $(PATHPRE) $(MAKE) $(INSTALLPAR) -f $< -C $(<D) $(INSTALLCMD) $(MAKECMD.$(notdir $@)) > $(<D)/log.install 2>&1
 474         @touch $@
 475         @echo 'done'
 476 
 477 ##########################################################################################
 478 
 479 $(PREFIX)/devkit.info: FRC
 480         @echo 'Creating devkit.info in the root of the kit'
 481         rm -f $@
 482         touch $@
 483         echo '# This file describes to configure how to interpret the contents of this' >> $@
 484         echo '# devkit' >> $@
 485         echo '' >> $@
 486         echo 'DEVKIT_NAME="$(gcc_ver) - $(LINUX_VERSION)"' >> $@
 487         echo 'DEVKIT_TOOLCHAIN_PATH="$$DEVKIT_ROOT/bin"' >> $@
 488         echo 'DEVKIT_SYSROOT="$$DEVKIT_ROOT/$$host/sysroot"' >> $@
 489 
 490 ##########################################################################################
 491 
 492 ifeq ($(TARGET), $(HOST))
 493   $(PREFIX)/bin/%:
 494         @echo 'Creating missing $* soft link'
 495         ln -s $(TARGET)-$* $@
 496 
 497   missing-links := $(addprefix $(PREFIX)/bin/, \
 498       addr2line ar as c++ c++filt elfedit g++ gcc gprof ld nm objcopy ranlib readelf \
 499       size strings strip)
 500 endif
 501 
 502 ##########################################################################################
 503 
 504 bfdlib : $(bfdlib)
 505 binutils : $(binutils)
 506 rpms : $(rpms)
 507 libs : $(libs)
 508 sysroot : rpms libs
 509 gcc : sysroot $(gcc) $(gccpatch)
 510 all : binutils gcc bfdlib $(PREFIX)/devkit.info $(missing-links) $(SYSROOT_LINKS)
 511 
 512 # this is only built for host. so separate.
 513 ccache : $(ccache)
 514 
 515 # Force target
 516 FRC:
 517 
 518 .PHONY : gcc all binutils bfdlib link_libs rpms libs sysroot