1 ################################################################################
3 # cross.cygclass - for building cross-compiled packages
5 # Part of cygport - Cygwin packaging application
6 # Copyright (C) 2006-2020 Cygport authors
7 # Provided by the Cygwin project <https://cygwin.com/>
9 # cygport is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # cygport is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with cygport. If not, see <https://www.gnu.org/licenses/>.
22 ################################################################################
24 #****h* Cygclasses/cross.cygclass
29 # This cygclass assists in building libraries to be used by "cross-compilers",
30 # which generate code to be run on a different platform (the "host" system)
31 # then the one on which it was built (and, in most cases, NOT on the build
32 # system), but can be used on the build system to cross-compile other software
33 # which depends on it.
35 # Supported build systems for cross-compiling are autotools, cmake, qmake, and
36 # custom Makefiles. When using cross.cygclass, cygconf, cygcmake, and cygqmake4
37 # automatically use the necessary arguments for cross-compiling, and cyginstall
38 # and most of the do* and new* installation functions install into the sysroot
39 # instead of the native root. Note that those installation functions which
40 # accept an absolute path (dodir, exeinto, insinto, dosym) make no assumptions
44 #****v* cross.cygclass/CROSS_HOST
46 # The CPU-VENDOR-OS[-TOOLCHAIN] triplet, or customary abbreviation thereof,
47 # describing the system for which you are cross-compiling; for example:
48 # * i686-linux [i686-pc-linux-gnu]: i686 GNU/Linux
49 # * x86_64-w64-mingw32: Win64
50 # * arm-eabi [arm-unknown-eabi]: Embedded ARM ELF system (e.g. with Newlib)
51 # * avr [avr-unknown-none]: Atmel AVR bare metal (e.g. Arduino)
52 # Note that while 'unknown' vendor tags are often omitted, particularly in
53 # conjunction with a 'none' OS tag, as above, this does not apply to e.g.
54 # 'armv7hl-unknown-linux-gnueabi' and other systems.
56 # * CROSS_HOST must be defined before inherit()ing cross.cygclass.
57 # * CROSS_HOST must match the prefix of the toolchain being used (in other
58 # words, the TOOLCHAIN_TARGET used for building said toolchain).
60 if ! defined CROSS_HOST
62 error "CROSS_HOST must be defined before 'inherit cross'"
65 __check_triplet ${CROSS_HOST}
69 unset ARCH_i686 ARCH_x86_64
72 #****d* cross.cygclass/CROSS_SYSROOT
74 # GCC cross-compilers provide the concept of a sysroot: a directory which
75 # acts as the virtual root of the target filesystem, resulting in
76 # CROSS_SYSROOT/$prefix/include and CROSS_SYSROOT/$prefix/lib being in the
77 # default search paths without requiring -I or -L flags.
79 # This means that software natively compiled for the host system can be
80 # unpacked into the sysroot and used for cross-compiling. This is
81 # especially useful when bootstrapping a new toolchain; e.g. for Linux, one
82 # can just download glibc and kernel headers packages from a Linux distro,
83 # unpack them into the CROSS_SYSROOT, and use those to build a cross-GCC
86 # If you do use natively-compiled host packages under the sysroot, you must
87 # fix paths in all *-config scripts, .la libtool libraries, and .pc
88 # pkg-config files so they do not erroneously point to Cygwin-native
89 # headers and libraries.
91 CROSS_SYSROOT="/usr/${CHOST}/sys-root"
93 #****d* cross.cygclass/CROSS_PREFIX
95 # The $prefix directory under the sysroot. This definition is meant to
96 # be used in configure or cmake arguments to avoid dealing with different
97 # prefixes on various systems.
99 CROSS_PREFIX="$(__host_prefix)"
101 #****d* cross.cygclass/CROSS_BINDIR
103 # The $prefix/bin directory under the sysroot. This definition is meant to
104 # be used in configure or cmake arguments to avoid dealing with different
105 # prefixes on various systems.
107 CROSS_BINDIR="$(__host_prefix)/bin"
109 #****d* cross.cygclass/CROSS_INCLUDEDIR
111 # The $prefix/include directory under the sysroot. This definition is
112 # meant to be used in configure or cmake arguments to avoid dealing with
113 # different prefixes on various systems.
115 CROSS_INCLUDEDIR="$(__host_prefix)/include"
117 #****d* cross.cygclass/CROSS_LIBDIR
119 # The $prefix/lib directory under the sysroot. This definition is meant to
120 # be used in configure or cmake arguments to avoid dealing with different
121 # prefixes on various systems.
123 CROSS_LIBDIR="$(__host_prefix)/lib"
125 #****d* cross.cygclass/CROSS_DATADIR
127 # The $prefix/share directory under the sysroot. This definition is meant to
128 # be used in configure or cmake arguments to avoid dealing with different
129 # prefixes on various systems.
131 CROSS_DATADIR="$(__host_prefix)/share"
135 F77="${CHOST}-gfortran"
136 FC="${CHOST}-gfortran"
139 OBJCXX="${CHOST}-g++"
140 PKG_CONFIG="${CHOST}-pkg-config"
142 CC_SYSROOT=$(${CC} -print-sysroot)
144 # native binutils do not install $host- prefixed equivalents, so we must not
145 # prefix them when "crossbacking" (e.g. building i686-cygwin sysroot libs on
146 # i686-cygwin for use on x86_64-cygwin)
151 OBJDUMP="${CHOST}-objdump"
152 RANLIB="${CHOST}-ranlib"
153 STRIP="${CHOST}-strip"
156 # windres is not provided for all platforms
158 *-cygwin*|*-mingw*|*-msys*|*-*-pe*|*-wince*|*-cegcc*|*-netbsdpe)
161 DLLTOOL="${CHOST}-dlltool"
162 RC="${CHOST}-windres"
171 # GCC since 4.7 uses -mms-bitfields by default, but does not have
172 # builtin stack-smashing support
173 CFLAGS="-ggdb -O2 -pipe -Wall -Werror=format-security"
178 OBJCXXFLAGS=${CXXFLAGS}
181 LDFLAGS="${LDFLAGS} -Wl,--as-needed -Wl,--no-undefined"
185 if ! check_tool gcc || ! check_tool strip
187 error "This package requires ${CHOST} binutils and gcc"
190 #****I* cross.cygclass/doelflib
192 # doelflib libfoo.so.X.Y.Z [...]
194 # Installs the given ELF libraries into the CROSS_SYSROOT libdir and creates
195 # the symlinks for the SONAME (usually .so.X) and the link library (.so).
197 # Only the real library file should be passed to doelflib, not the symlinks
198 # usually created in the build directory.
201 local clibdir="$(__host_prefix)/lib"
202 local i soname sonames
210 error "file ${i} does not exist"
215 *) error "doelflib: ${i}: not an ELF shared object" ;;
218 soname=$(${OBJDUMP} -p ${i} | sed -n -e 's| *SONAME *\([^ ]*\).*|\1|gp')
220 # try to catch mistaken "doelflib libfoo.so*"
221 if echo "${sonames}" | grep -q ${soname}
223 error "SONAME ${soname} has already been installed"
225 sonames+=" ${soname} "
228 __doinstall 0755 ${i} ${clibdir} || error "doelflib ${i} failed"
230 if test ${i##*/} != ${soname}
232 dosym ${i##*/} ${clibdir}/${soname}
234 if test ${soname%.so*}.so != ${soname}
236 dosym ${i##*/} ${clibdir}/${soname%.so*}.so
241 #****I* cross.cygclass/cross_sysrootize
243 # cross_sysrootize FILE [FILE] ...
245 # Adds CROSS_SYSROOT to all paths in given text file(s). Paths should
246 # be specified as if on the system, without a leading $D.
250 local prefix=$(__target_prefix)
254 if [ ! -e ${D}${f#${D}} ]
256 error "cross_sysrootize: $f: file not found"
259 # the [^t] should catch paths already containing the sysroot
260 sed -i -e "s|\([^t]\)${prefix}|\1${CROSS_PREFIX}|g" ${D}${f#${D}} || error "$FUNCNAME: $f: sed failed"
264 #****I* cross.cygclass/cross_desysrootize
266 # cross_desysrootize FILE [FILE] ...
268 # Removes CROSS_SYSROOT from all paths in given text file(s). Paths should
269 # be specified as if on the system, without a leading $D.
271 cross_desysrootize(){
276 if [ ! -e ${D}${f#${D}} ]
278 error "cross_desysrootize: $f: file not found"
281 sed -i -e "s|${CROSS_SYSROOT}||g" ${D}${f#${D}} || error "$FUNCNAME: $f: sed failed"
285 readonly -f doelflib cross_sysrootize cross_desysrootize