Difference between revisions of "MinGW package guidelines"

From ArchWiki
Jump to: navigation, search
(CMake (note: broken example, have solution but will edit later))
(Autotools)
(46 intermediate revisions by 4 users not shown)
Line 3: Line 3:
  
 
This page expains how to write [[PKGBUILD]]s for software running on Windows using GCC. There are two options to build software for Windows on Linux:
 
This page expains how to write [[PKGBUILD]]s for software running on Windows using GCC. There are two options to build software for Windows on Linux:
* [[#MinGW.org|[www.MinGW.org]]]: provides 32-bit toolchains with limited DirectX support. Available from Arch's [[#.5Bcommunity.5D|[community]]] repository by installing {{Pkg|mingw32-gcc}}.
+
 
* [[#MinGW-w64|[mingw-w64.sourceforge.net]]]: provides 32 and 64-bit toolchains with secure crt, Vista+ API, DDK (ReactOS), and DirectX (WINE) support. For a full list of supported features and differences with the old MinGW.org, see [http://sourceforge.net/apps/trac/mingw-w64/wiki/Feature%20list here]. Available in the [[AUR]] by installing {{AUR|mingw-w64-gcc}}
+
* [[#MinGW-w64|[mingw-w64.sourceforge.net]]]: provides 32 and 64-bit toolchains with secure crt, Vista+ API, DDK (ReactOS), and DirectX (WINE) support. For a full list of supported features and differences with the old MinGW.org, see [http://sourceforge.net/apps/trac/mingw-w64/wiki/Feature%20list here]. Available from Arch's [[#.5Bcommunity.5D|[community]]] repository by installing {{Pkg|mingw-w64-gcc}}.
 +
* [[#MinGW.org|[www.MinGW.org]]]: provides 32-bit toolchains with limited DirectX support. Available in the [[AUR]] by installing {{AUR|mingw32-gcc}}
  
 
There are some differences between the packaging of both projects, detailed below.
 
There are some differences between the packaging of both projects, detailed below.
 +
 +
 +
== MinGW-w64 ==
 +
 +
=== Package naming ===
 +
A package for mingw-w64 should be named {{Ic|mingw-w64-''pkgname''}}. If a static variant of the package is being built, suffix the package name with {{ic|-static}} (see below for the cases where this is necessary).
 +
 +
=== Packaging ===
 +
Packaging for cross platform packages can be fairly tricky as there are many different build systems and low-level quirks. Take a note of the following things though:
 +
* always add {{AUR|mingw-w64-crt}} to {{ic|depends}}
 +
* always add {{AUR|mingw-w64-gcc}} to {{ic|makedepends}}
 +
* always add {{ic|!strip}}, {{ic|staticlibs}} and {{ic|!buildflags}} to {{ic|options }}
 +
* always use the original {{ic|pkgdesc}} and append {{ic|(mingw-w64)}} to the end of {{ic|pkgdesc}}
 +
* always use and follow the original {{ic|pkgver}} of the official package
 +
* always add {{ic|unset LDFLAGS CPPFLAGS}} in front of toolchain configure/Makefile generation sequences
 +
* always build both 32-bit and 64-bit versions of libraries, unless of course the package can only target, or is meant to only target, 32-bit or 64-bit, or if problems arise building one of the two.
 +
* always put all stuff under the {{Ic|/usr/i686-w64-mingw32}} and {{Ic|/usr/x86_64-w64-mingw32}} prefix
 +
* always use {{ic|any}} as the architecture
 +
* always build both shared and static binaries, unless they conflict
 +
* always remove Win32 executables (*.exe) if the intended package is a library ({{ic|<nowiki>rm "$pkgdir"/usr/${_arch}/bin/*.exe</nowiki>}})
 +
* consider removing unneeded documentation ({{ic|<nowiki>rm -r $pkgdir/usr/i686-w64-mingw32/share/{doc,info,man}</nowiki>}}, {{ic|<nowiki>rm -r $pkgdir/usr/x86_64-w64-mingw32/share/{doc,info,man}</nowiki>}})
 +
* consider using {{AUR|mingw-w64-pkg-config}} for building with configure scripts
 +
* consider using {{AUR|mingw-w64-cmake}} for building with CMake
 +
* consider explicitly stripping symbols with {{ic|${_arch}-strip}} in {{ic|package()}}'s for-loop as demonstrated in the below PKGBUILD examples.
 +
** consider using the `find` command to iterate over {{ic|$pkgdir}} since not all DLLs, static libraries, or executables may reside in their appropriate locations.
 +
*** if the binary is a DLL, use {{ic|${_arch}-strip --strip-unneeded *.dll}}
 +
*** if the binary is a static lib, use {{ic|${_arch}-strip -g *.a}}
 +
* if a package is modular (requires certain build dependencies, but said dependencies are optional to the end user) add these to {{ic|makedepends}} and {{ic|optdepends}}. Be sure to subtract them from {{ic|depends}} if updating an existing package. Example of this in use: {{AUR|mingw-w64-ruby}}, {{AUR|mingw-w64-allegro}}
 +
* if a package installs a {{ic|$pkgdir/usr/${_arch}/bin/*-config}} script, symlink it to {{ic|$pkgdir/usr/bin/${_arch}-*-config}}
 +
 +
As mentioned above, the files should all be installed into {{Ic|/usr/i686-w64-mingw32}} and {{Ic|/usr/x86_64-w64-mingw32}}. Specifically, all DLLs should be put into {{Ic|/usr/*-w64-mingw32/bin}} as they are dynamic libraries needed at runtime. Their corresponding {{Ic|.dll.a}} files should go into {{Ic|/usr/*-w64-mingw32/lib}}. Please delete any unnecessary documentation and perhaps other files from {{Ic|/usr/share}}. Cross-compilations packages are not meant for the user but only for the compiler and binary distribution, and as such you should try to make them as small as possible.
 +
 +
Always try to match the {{Ic|pkgver}} in your mingw-w64 packages to the {{Ic|pkgver}} of the corresponding regular packages in the official Arch Linux repos (not the testing repos). This ensures that the cross-compiled software works exactly the same way on Windows without any unexpected bugs. If packages in Arch are out-of-date, there usually is a good reason and you should still follow the Arch version instead of using the most recent upstream release. If the corresponding native package is in the AUR, you need not follow this version policy, as many AUR packages are often orphaned or left unmaintained.
 +
 +
=== Examples ===
 +
The following examples will try to cover some of the most common conventions and build systems.
 +
 +
==== Autotools ====
 +
{{bc|<nowiki>
 +
# Maintainer: yourname <yourmail>
 +
pkgname=mingw-w64-foo
 +
pkgver=1.0
 +
pkgrel=1
 +
pkgdesc="Foo bar (mingw-w64)"
 +
arch=('any')
 +
url="http://www.foo.bar"
 +
license=('GPL')
 +
makedepends=('mingw-w64-gcc')
 +
depends=('mingw-w64-crt')
 +
options=('!strip' '!buildflags' 'staticlibs')
 +
source=("http://www.foo.bar/foobar.tar.gz")
 +
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')
 +
 +
_architectures="i686-w64-mingw32 x86_64-w64-mingw32"
 +
 +
build() {
 +
  unset LDFLAGS
 +
  cd "${srcdir}/foo-$pkgver/"
 +
  for _arch in ${_architectures}; do
 +
    mkdir -p build-${_arch} && pushd build-${_arch}
 +
    ../configure \
 +
      --prefix=/usr/${_arch} \
 +
      --host=${_arch}
 +
    make
 +
    popd
 +
  done
 +
}
 +
 +
package() {
 +
  for _arch in ${_architectures}; do
 +
    cd "${srcdir}/foo-$pkgver/build-${_arch}"
 +
    make DESTDIR="${pkgdir}" install
 +
    find "$pkgdir" -name '*.exe' -o -name '*.bat' -o -name '*.def' -o -name '*.exp' -o -name '*.manifest' -exec rm {} \;
 +
    find "$pkgdir" -name '*.dll' -exec ${_arch}-strip --strip-unneeded {} \;
 +
    find "$pkgdir" -name '*.dll' -o -name '*.a' -exec ${_arch}-strip -g {} \;
 +
  done
 +
}
 +
</nowiki>}}
 +
 +
==== CMake ====
 +
{{bc|<nowiki>
 +
# Maintainer: yourname <yourmail>
 +
pkgname=mingw-w64-foo
 +
pkgver=1.0
 +
pkgrel=1
 +
pkgdesc="Foo bar (mingw-w64)"
 +
arch=('any')
 +
url="http://www.foo.bar"
 +
license=('GPL')
 +
makedepends=('mingw-w64-cmake')
 +
depends=('mingw-w64-crt')
 +
options=('!strip' '!buildflags' 'staticlibs')
 +
source=("http://www.foo.bar/foobar.tar.gz")
 +
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')
 +
 +
_architectures="i686-w64-mingw32 x86_64-w64-mingw32"
 +
 +
build() {
 +
  unset LDFLAGS
 +
  cd "$srcdir/foo-$pkgver/"
 +
  for _arch in ${_architectures}; do
 +
    mkdir -p build-${_arch} && pushd build-${_arch}
 +
    ${_arch}-cmake \
 +
      -DCMAKE_BUILD_TYPE=Release \
 +
      ..
 +
    make
 +
    popd
 +
  done
 +
}
 +
 +
package() {
 +
  for _arch in ${_architectures}; do
 +
    cd "${srcdir}/foo-$pkgver/build-${_arch}"
 +
    make DESTDIR="${pkgdir}" install
 +
    find "$pkgdir" -name '*.exe' -o -name '*.bat' -o -name '*.def' -o -name '*.exp' -o -name '*.manifest' -exec rm {} \;
 +
    find "$pkgdir" -name '*.dll' -exec ${_arch}-strip --strip-unneeded {} \;
 +
    find "$pkgdir" -name '*.dll' -o -name '*.a' -exec ${_arch}-strip -g {} \;
 +
  done
 +
}
 +
</nowiki>}}
  
 
== MinGW.org==
 
== MinGW.org==
  
=== Package Naming ===
+
=== Package naming ===
 
A package for mingw32 should be named {{Ic|mingw32-''pkgname''}}. If a static variant of the package is being built, suffix the package name with {{ic|-static}}.
 
A package for mingw32 should be named {{Ic|mingw32-''pkgname''}}. If a static variant of the package is being built, suffix the package name with {{ic|-static}}.
  
Line 17: Line 138:
 
* always add {{Pkg|mingw32-runtime}} to {{ic|depends}}
 
* always add {{Pkg|mingw32-runtime}} to {{ic|depends}}
 
* always add {{Pkg|mingw32-gcc}} to {{ic|makedepends}}
 
* always add {{Pkg|mingw32-gcc}} to {{ic|makedepends}}
* always add {{ic|!strip}} and {{ic|!buildflags}} to {{ic|options }}
+
* always add {{ic|!strip}}, {{ic|staticlibs}} and {{ic|!buildflags}} to {{ic|options }}
 
* always use the original {{ic|pkgdesc}} and append {{ic|(mingw32)}} to the end of {{ic|pkgdesc}}
 
* always use the original {{ic|pkgdesc}} and append {{ic|(mingw32)}} to the end of {{ic|pkgdesc}}
 
* always use and follow the original {{ic|pkgver}} of the official package
 
* always use and follow the original {{ic|pkgver}} of the official package
* always add {{ic|unset LDFLAGS}} in front of toolchain configure/Makefile generation sequences
+
* always add {{ic|unset LDFLAGS CPPFLAGS}} in front of toolchain configure/Makefile generation sequences
 
* always put all stuff under the {{Ic|/usr/i486-mingw32}} prefix
 
* always put all stuff under the {{Ic|/usr/i486-mingw32}} prefix
 
* always use {{ic|any}} as the architecture
 
* always use {{ic|any}} as the architecture
 
* always build both shared and static binaries, unless they conflict
 
* always build both shared and static binaries, unless they conflict
 +
* always remove Win32 executables (*.exe) if the intended package is a library
 
* consider using {{ic|!libtool}} in {{ic|options}} if you get {{Ic|.la}} files in {{ic|$pkgdir}}
 
* consider using {{ic|!libtool}} in {{ic|options}} if you get {{Ic|.la}} files in {{ic|$pkgdir}}
 
* consider removing unneeded documentation ({{ic|<nowiki>rm -r $pkgdir/usr/i486-mingw32/share/{doc,info,man}</nowiki>}})
 
* consider removing unneeded documentation ({{ic|<nowiki>rm -r $pkgdir/usr/i486-mingw32/share/{doc,info,man}</nowiki>}})
 +
* consider explicitly stripping symbols with {{ic|i486-mingw32-strip}} in {{ic|package()}}
 +
** if the binary is a DLL, use {{ic|i486-mingw32-strip -x -g *.dll}}
 +
** if the binary is a static lib, use {{ic|i486-mingw32-strip -g *.a}}
  
 
As mentioned above, the files should all be installed into {{Ic|/usr/i486-mingw32}}. Specifically, all {{Ic|.dll}} files should be put into {{Ic|/usr/i486-mingw32/bin}} as they are dynamic libraries needed at runtime. Their corresponding {{Ic|.dll.a}} files should go into {{Ic|/usr/i486-mingw32/lib}}. Please delete any unnecessary documentation and perhaps other files from {{Ic|/usr/share}}. Cross-compilations packages are not meant for the user but only for the compiler and as such you should try to make them as small as possible.
 
As mentioned above, the files should all be installed into {{Ic|/usr/i486-mingw32}}. Specifically, all {{Ic|.dll}} files should be put into {{Ic|/usr/i486-mingw32/bin}} as they are dynamic libraries needed at runtime. Their corresponding {{Ic|.dll.a}} files should go into {{Ic|/usr/i486-mingw32/lib}}. Please delete any unnecessary documentation and perhaps other files from {{Ic|/usr/share}}. Cross-compilations packages are not meant for the user but only for the compiler and as such you should try to make them as small as possible.
Line 46: Line 171:
 
makedepends=('mingw32-gcc')
 
makedepends=('mingw32-gcc')
 
depends=('mingw32-runtime')
 
depends=('mingw32-runtime')
options=('!strip' '!buildflags')
+
options=('!strip' '!buildflags' 'staticlibs')
 
source=("http://www.foo.bar/foobar.tar.gz")
 
source=("http://www.foo.bar/foobar.tar.gz")
 
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')
 
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')
Line 70: Line 195:
 
   cd "${srcdir}/foobar"
 
   cd "${srcdir}/foobar"
  
   mkdir -p ${pkgdir}/usr/i486-mingw32/{lib,include}
+
   mkdir -p "${pkgdir}"/usr/i486-mingw32/{lib,include}
 
   make install
 
   make install
   chmod 644 ${pkgdir}/usr/i486-mingw32/lib/libfoo.a
+
   chmod 644 "${pkgdir}"/usr/i486-mingw32/lib/libfoo.a
   i486-mingw32-ranlib ${pkgdir}/usr/i486-mingw32/lib/libfoo.a
+
   i486-mingw32-ranlib "${pkgdir}"/usr/i486-mingw32/lib/libfoo.a
 +
 
 +
  i486-mingw32-strip -x -g "${pkgdir}"/usr/i486-mingw32/bin/*.dll
 +
  i486-mingw32-strip -g "${pkgdir}"/usr/i486-mingw32/lib/*.a
 
}
 
}
 
</nowiki>}}
 
</nowiki>}}
Line 89: Line 217:
 
makedepends=('mingw32-gcc')
 
makedepends=('mingw32-gcc')
 
depends=('mingw32-runtime')
 
depends=('mingw32-runtime')
options=('!strip' '!buildflags')
+
options=('!strip' '!buildflags' 'staticlibs')
 
source=("http://www.foo.bar/foobar.tar.gz")
 
source=("http://www.foo.bar/foobar.tar.gz")
 
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')
 
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')
Line 106: Line 234:
  
 
   make DESTDIR=${pkgdir} install
 
   make DESTDIR=${pkgdir} install
 +
  i486-mingw32-strip -x -g "${pkgdir}"/usr/i486-mingw32/bin/*.dll
 +
  i486-mingw32-strip -g "${pkgdir}"/usr/i486-mingw32/lib/*.a
 
}
 
}
 
</nowiki>}}
 
</nowiki>}}
Line 119: Line 249:
 
url="http://www.foo.bar"
 
url="http://www.foo.bar"
 
license=('GPL')
 
license=('GPL')
makedepends=('mingw32-gcc' 'cmake')
+
makedepends=('mingw32-cmake')
 
depends=('mingw32-runtime')
 
depends=('mingw32-runtime')
options=('!strip' '!buildflags')
+
options=('!strip' '!buildflags' 'staticlibs')
 
source=("http://www.foo.bar/foobar.tar.gz"
 
source=("http://www.foo.bar/foobar.tar.gz"
 
         "cmake-toolchain.cmake")
 
         "cmake-toolchain.cmake")
Line 128: Line 258:
  
 
build() {
 
build() {
   cd "${srcdir}/foobar"
+
   cd "${srcdir}/foobar-$pkgver"
+
 
   unset LDFLAGS
 
   unset LDFLAGS
 
 
   mkdir build && cd build
 
   mkdir build && cd build
   cmake .. \
+
   i486-mingw32-cmake \
     -DCMAKE_INSTALL_PREFIX=/usr/i486-mingw32/ \
+
     -DCMAKE_BUILD_TYPE=Release
     -DCMAKE_TOOLCHAIN_FILE=${srcdir}/cmake-toolchain.cmake
+
     ..
 
   make
 
   make
 
}
 
}
  
 
package() {
 
package() {
   cd "${srcdir}/foobar"
+
   cd "${srcdir}/foobar-$pkgver"
 
+
 
   cd build
 
   cd build
   make DESTDIR=${pkgdir} install
+
   make DESTDIR="${pkgdir}" install
}
+
  i486-mingw32-strip -x -g "${pkgdir}"/usr/i486-mingw32/bin/*.dll
</nowiki>}}
+
   i486-mingw32-strip -g "${pkgdir}"/usr/i486-mingw32/lib/*.a
 
+
In this case, the toolchain is specified externally in {{ic|cmake-toolchain.cmake}}:
+
 
+
{{hc|cmake-toolchain.cmake|<nowiki># the name of the target operating system
+
SET(CMAKE_SYSTEM_NAME Windows)
+
 
+
# which compilers to use for C and C++
+
SET(CMAKE_C_COMPILER i486-mingw32-gcc)
+
SET(CMAKE_CXX_COMPILER i486-mingw32-g++)
+
SET(CMAKE_RC_COMPILER i486-mingw32-windres)
+
 
+
# here is the target environment located
+
SET(CMAKE_FIND_ROOT_PATH /usr/i486-mingw32)
+
 
+
# adjust the default behaviour of the FIND_XXX() commands:
+
# search headers and libraries in the target environment, search
+
# programs in the host environment
+
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)</nowiki>}}
+
 
+
== MinGW-w64 ==
+
 
+
=== Package Naming ===
+
A package for mingw-w64 should be named {{Ic|mingw-w64-''pkgname''}}. In the case that a package is 32-bit only, prefix the package name with {{Ic|lib32-}}. If a static variant of the package is being built, suffix the package name with {{ic|-static}}.
+
 
+
=== Packaging ===
+
Packaging for cross platform packages can be fairly tricky as there are many different build systems and low-level quirks. Take a note of the following things though:
+
* always add {{AUR|mingw-w64-crt}} to {{ic|depends}}
+
* always add {{AUR|mingw-w64-gcc}} to {{ic|makedepends}}
+
* always add {{ic|!strip}} and {{ic|!buildflags}} to {{ic|options }}
+
* always use the original {{ic|pkgdesc}} and append {{ic|(mingw-w64)}} to the end of {{ic|pkgdesc}}
+
* always use and follow the original {{ic|pkgver}} of the official package
+
* always add {{ic|unset LDFLAGS}} in front of toolchain configure/Makefile generation sequences
+
* always build both 32-bit and 64-bit versions of libraries, unless of course the package can only target, or is meant to only target, 32-bit or 64-bit, or if problems arise building one of the two.
+
* always put all stuff under the {{Ic|/usr/i686-w64-mingw32}} and {{Ic|/usr/x86_64-w64-mingw32}} prefix
+
* always use {{ic|any}} as the architecture
+
* always build both shared and static binaries, unless they conflict
+
* consider using {{ic|!libtool}} in {{ic|options}} if you get {{Ic|.la}} files in {{ic|$pkgdir}}
+
* consider removing unneeded documentation ({{ic|<nowiki>rm -r $pkgdir/usr/i686-w64-mingw32/share/{doc,info,man}</nowiki>}}, {{ic|<nowiki>rm -r $pkgdir/usr/x86_64-w64-mingw32/share/{doc,info,man}</nowiki>}})
+
* consider explicitly stripping symbols with {{ic|${_arch}-strip}} in {{ic|package()}}'s for-loop as demonstrated in the below PKGBUILD examples.
+
** if the binary is a DLL, use {{ic|${_arch}-strip -x -g *.dll}}
+
** if the binary is a static lib, use {{ic|${_arch}-strip -g *.a}}
+
** if the binary is an executable, use {{ic|${_arch}-strip *.exe}} (in some cases, .exe is not appended, so you must specify the file name instead of a wildcard)
+
 
+
As mentioned above, the files should all be installed into {{Ic|/usr/i686-w64-mingw-w64}} and {{Ic|/usr/x86_64-w64-mingw32}}. Specifically, all {{Ic|.dll}} files should be put into {{Ic|/usr/*-w64-mingw-w64/bin}} as they are dynamic libraries needed at runtime. Their corresponding {{Ic|.dll.a}} files should go into {{Ic|/usr/*-w64-mingw-w64/lib}}. Please delete any unnecessary documentation and perhaps other files from {{Ic|/usr/share}}. Cross-compilations packages are not meant for the user but only for the compiler and as such you should try to make them as small as possible.
+
 
+
Always try to match the {{Ic|pkgver}} in your mingw-w64 packages to the {{Ic|pkgver}} of the corresponding regular packages in the official Arch Linux repos (not the testing repos). This ensures that the cross-compiled software works exactly the same way on Windows without any unexpected bugs. If packages in Arch are out-of-date, there usually is a good reason and you should still follow the Arch version instead of using the most recent upstream release.
+
 
+
=== Examples ===
+
The following examples will try to cover some of the most common conventions and build systems.
+
 
+
==== Autotools ====
+
{{bc|<nowiki>
+
# Maintainer: yourname <yourmail>
+
pkgname=mingw-w64-foo
+
pkgver=1.0
+
pkgrel=1
+
pkgdesc="Foo bar (mingw-w64)"
+
arch=('any')
+
url="http://www.foo.bar"
+
license=('GPL')
+
makedepends=('mingw-w64-gcc')
+
depends=('mingw-w64-crt')
+
options=('!strip' '!buildflags')
+
source=("http://www.foo.bar/foobar.tar.gz")
+
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')
+
 
+
_architectures="i686-w64-mingw32 x86_64-w64-mingw32"
+
 
+
build() {
+
   unset LDFLAGS
+
 
+
  for _arch in ${_architectures}; do
+
    mkdir -p ${srcdir}/foo-build-${_arch}
+
    cd ${srcdir}/gcc-build-${_arch} 
+
 
+
    ${srcdir}/foo/configure --prefix=/usr/${_arch} --host=${_arch}
+
    make
+
  done
+
}
+
 
+
package() {
+
  for _arch in ${_targets}; do
+
    make DESTDIR=${pkgdir} install
+
  done
+
}
+
</nowiki>}}
+
 
+
==== CMake ====
+
{{hc|Toolchain-i686-mingw-w64-mingw32.cmake|<nowiki>
+
SET(CMAKE_SYSTEM_NAME Windows)
+
 
+
# specify the cross compiler
+
SET(CMAKE_C_COMPILER /usr/bin/i686-w64-mingw32-gcc)
+
SET(CMAKE_CXX_COMPILER /usr/bin/i686-w64-mingw32-g++)
+
 
+
# where is the target environment
+
SET(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32)
+
 
+
# search for programs in the build host directories
+
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+
# for libraries and headers in the target directories
+
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+
 
+
# Make sure Qt can be detected by CMake
+
SET(QT_BINARY_DIR /usr/i686-w64-mingw32/bin /usr/bin)
+
 
+
# set the resource compiler (RHBZ #652435)
+
SET(CMAKE_RC_COMPILER /usr/bin/i686-w64-mingw32-windres)
+
 
+
# override boost library suffix which defaults to -mgw
+
SET(Boost_COMPILER -gcc47)
+
 
+
# These are needed for compiling lapack (RHBZ #753906)
+
SET(CMAKE_Fortran_COMPILER /usr/bin/i686-w64-mingw32-gfortran)
+
SET(CMAKE_AR:FILEPATH /usr/bin/i686-w64-mingw32-ar)
+
SET(CMAKE_RANLIB:FILEPATH /usr/bin/i686-w64-mingw32-ranlib)
+
</nowiki>}}
+
 
+
{{hc|Toolchain-x86_64-mingw-w64-mingw32.cmake|<nowiki>
+
SET(CMAKE_SYSTEM_NAME Windows)
+
 
+
# specify the cross compiler
+
SET(CMAKE_C_COMPILER /usr/bin/x86_64-w64-mingw32-gcc)
+
SET(CMAKE_CXX_COMPILER /usr/bin/x86_64-w64-mingw32-g++)
+
 
+
# where is the target environment
+
SET(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32)
+
 
+
# search for programs in the build host directories
+
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
+
# for libraries and headers in the target directories
+
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+
 
+
# Make sure Qt can be detected by CMake
+
SET(QT_BINARY_DIR /usr/x86_64-w64-mingw32/bin /usr/bin)
+
 
+
# set the resource compiler (RHBZ #652435)
+
SET(CMAKE_RC_COMPILER /usr/bin/x86_64-w64-mingw32-windres)
+
 
+
# override boost library suffix which defaults to -mgw
+
SET(Boost_COMPILER -gcc47)
+
 
+
# These are needed for compiling lapack (RHBZ #753906)
+
SET(CMAKE_Fortran_COMPILER /usr/bin/x86_64-w64-mingw32-gfortran)
+
SET(CMAKE_AR:FILEPATH /usr/bin/x86_64-w64-mingw32-ar)
+
SET(CMAKE_RANLIB:FILEPATH /usr/bin/x86_64-w64-mingw32-ranlib)
+
</nowiki>}}
+
 
+
{{bc|<nowiki>
+
# Maintainer: yourname <yourmail>
+
pkgname=mingw-w64-foo
+
pkgver=1.0
+
pkgrel=1
+
pkgdesc="Foo bar (mingw-w64)"
+
arch=('any')
+
url="http://www.foo.bar"
+
license=('GPL')
+
makedepends=('mingw-w64-gcc' 'cmake')
+
depends=('mingw-w64-crt')
+
options=('!strip' '!buildflags')
+
source=("http://www.foo.bar/foobar.tar.gz"
+
        "cmake-toolchain.cmake")
+
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24'
+
        'dd2b2db48187dff84050fe191d309d81')
+
 
+
_architectures="i686-w64-mingw32 x86_64-w64-mingw32"
+
 
+
build() {
+
  unset LDFLAGS
+
 
+
for _arch in ${_architectures}; do
+
    mkdir build-${_arch} && pushd build-${_arch}
+
    PATH="/usr/${_arch}/bin:$PATH" cmake \
+
      -DCMAKE_VERBOSE_MAKEFILE=ON \
+
      -DCMAKE_INSTALL_PREFIX:PATH=/usr/${_arch} \
+
      -DCMAKE_INSTALL_LIBDIR:PATH=/usr/${_arch}/lib \
+
      -DINCLUDE_INSTALL_DIR:PATH=/usr/${_arch}/include \
+
      -DLIB_INSTALL_DIR:PATH=/usr/${_arch}/lib \
+
      -DSYSCONF_INSTALL_DIR:PATH=/usr/${_arch}/etc \
+
      -DSHARE_INSTALL_PREFIX:PATH=/usr/${_arch}/share \
+
      -DBUILD_SHARED_LIBS:BOOL=ON \
+
      -DCMAKE_TOOLCHAIN_FILE=${srcdir}/Toolchain-${_arch}.cmake \
+
      ..
+
    make
+
    popd
+
  done
+
}
+
 
+
package() {
+
  for _arch in ${_architectures}; do
+
    cd ${srcdir}/build-${_arch}
+
    make DESTDIR=${pkgdir} install
+
  done
+
 
}
 
}
 
</nowiki>}}
 
</nowiki>}}

Revision as of 13:25, 20 November 2013

Template:Package Guidelines

This page expains how to write PKGBUILDs for software running on Windows using GCC. There are two options to build software for Windows on Linux:

  • [mingw-w64.sourceforge.net]: provides 32 and 64-bit toolchains with secure crt, Vista+ API, DDK (ReactOS), and DirectX (WINE) support. For a full list of supported features and differences with the old MinGW.org, see here. Available from Arch's [community] repository by installing mingw-w64-gcc.
  • [www.MinGW.org]: provides 32-bit toolchains with limited DirectX support. Available in the AUR by installing mingw32-gccAUR

There are some differences between the packaging of both projects, detailed below.


MinGW-w64

Package naming

A package for mingw-w64 should be named mingw-w64-pkgname. If a static variant of the package is being built, suffix the package name with -static (see below for the cases where this is necessary).

Packaging

Packaging for cross platform packages can be fairly tricky as there are many different build systems and low-level quirks. Take a note of the following things though:

  • always add mingw-w64-crtAUR to depends
  • always add mingw-w64-gccAUR to makedepends
  • always add !strip, staticlibs and !buildflags to options
  • always use the original pkgdesc and append (mingw-w64) to the end of pkgdesc
  • always use and follow the original pkgver of the official package
  • always add unset LDFLAGS CPPFLAGS in front of toolchain configure/Makefile generation sequences
  • always build both 32-bit and 64-bit versions of libraries, unless of course the package can only target, or is meant to only target, 32-bit or 64-bit, or if problems arise building one of the two.
  • always put all stuff under the /usr/i686-w64-mingw32 and /usr/x86_64-w64-mingw32 prefix
  • always use any as the architecture
  • always build both shared and static binaries, unless they conflict
  • always remove Win32 executables (*.exe) if the intended package is a library (rm "$pkgdir"/usr/${_arch}/bin/*.exe)
  • consider removing unneeded documentation (rm -r $pkgdir/usr/i686-w64-mingw32/share/{doc,info,man}, rm -r $pkgdir/usr/x86_64-w64-mingw32/share/{doc,info,man})
  • consider using mingw-w64-pkg-configAUR for building with configure scripts
  • consider using mingw-w64-cmakeAUR for building with CMake
  • consider explicitly stripping symbols with ${_arch}-strip in package()'s for-loop as demonstrated in the below PKGBUILD examples.
    • consider using the `find` command to iterate over $pkgdir since not all DLLs, static libraries, or executables may reside in their appropriate locations.
      • if the binary is a DLL, use ${_arch}-strip --strip-unneeded *.dll
      • if the binary is a static lib, use ${_arch}-strip -g *.a
  • if a package is modular (requires certain build dependencies, but said dependencies are optional to the end user) add these to makedepends and optdepends. Be sure to subtract them from depends if updating an existing package. Example of this in use: mingw-w64-rubyAUR, mingw-w64-allegroAUR
  • if a package installs a $pkgdir/usr/${_arch}/bin/*-config script, symlink it to $pkgdir/usr/bin/${_arch}-*-config

As mentioned above, the files should all be installed into /usr/i686-w64-mingw32 and /usr/x86_64-w64-mingw32. Specifically, all DLLs should be put into /usr/*-w64-mingw32/bin as they are dynamic libraries needed at runtime. Their corresponding .dll.a files should go into /usr/*-w64-mingw32/lib. Please delete any unnecessary documentation and perhaps other files from /usr/share. Cross-compilations packages are not meant for the user but only for the compiler and binary distribution, and as such you should try to make them as small as possible.

Always try to match the pkgver in your mingw-w64 packages to the pkgver of the corresponding regular packages in the official Arch Linux repos (not the testing repos). This ensures that the cross-compiled software works exactly the same way on Windows without any unexpected bugs. If packages in Arch are out-of-date, there usually is a good reason and you should still follow the Arch version instead of using the most recent upstream release. If the corresponding native package is in the AUR, you need not follow this version policy, as many AUR packages are often orphaned or left unmaintained.

Examples

The following examples will try to cover some of the most common conventions and build systems.

Autotools

# Maintainer: yourname <yourmail>
pkgname=mingw-w64-foo
pkgver=1.0
pkgrel=1
pkgdesc="Foo bar (mingw-w64)"
arch=('any')
url="http://www.foo.bar"
license=('GPL')
makedepends=('mingw-w64-gcc')
depends=('mingw-w64-crt')
options=('!strip' '!buildflags' 'staticlibs')
source=("http://www.foo.bar/foobar.tar.gz")
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')

_architectures="i686-w64-mingw32 x86_64-w64-mingw32"

build() {
  unset LDFLAGS
  cd "${srcdir}/foo-$pkgver/"
  for _arch in ${_architectures}; do
    mkdir -p build-${_arch} && pushd build-${_arch}
    ../configure \
      --prefix=/usr/${_arch} \
      --host=${_arch}
    make
    popd
  done
}

package() {
  for _arch in ${_architectures}; do
    cd "${srcdir}/foo-$pkgver/build-${_arch}"
    make DESTDIR="${pkgdir}" install
    find "$pkgdir" -name '*.exe' -o -name '*.bat' -o -name '*.def' -o -name '*.exp' -o -name '*.manifest' -exec rm {} \;
    find "$pkgdir" -name '*.dll' -exec ${_arch}-strip --strip-unneeded {} \;
    find "$pkgdir" -name '*.dll' -o -name '*.a' -exec ${_arch}-strip -g {} \;
  done
}

CMake

# Maintainer: yourname <yourmail>
pkgname=mingw-w64-foo
pkgver=1.0
pkgrel=1
pkgdesc="Foo bar (mingw-w64)"
arch=('any')
url="http://www.foo.bar"
license=('GPL')
makedepends=('mingw-w64-cmake')
depends=('mingw-w64-crt')
options=('!strip' '!buildflags' 'staticlibs')
source=("http://www.foo.bar/foobar.tar.gz")
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')

_architectures="i686-w64-mingw32 x86_64-w64-mingw32"

build() { 
  unset LDFLAGS
  cd "$srcdir/foo-$pkgver/"
  for _arch in ${_architectures}; do
    mkdir -p build-${_arch} && pushd build-${_arch}
    ${_arch}-cmake \
      -DCMAKE_BUILD_TYPE=Release \
      ..
    make
    popd
  done
}

package() {
  for _arch in ${_architectures}; do
    cd "${srcdir}/foo-$pkgver/build-${_arch}"
    make DESTDIR="${pkgdir}" install
    find "$pkgdir" -name '*.exe' -o -name '*.bat' -o -name '*.def' -o -name '*.exp' -o -name '*.manifest' -exec rm {} \;
    find "$pkgdir" -name '*.dll' -exec ${_arch}-strip --strip-unneeded {} \;
    find "$pkgdir" -name '*.dll' -o -name '*.a' -exec ${_arch}-strip -g {} \;
  done
}

MinGW.org

Package naming

A package for mingw32 should be named mingw32-pkgname. If a static variant of the package is being built, suffix the package name with -static.

Packaging

Packaging for cross platform packages can be fairly tricky as there are many different build systems and low-level quirks. Take a note of the following things though:

  • always add mingw32-runtime to depends
  • always add mingw32-gcc to makedepends
  • always add !strip, staticlibs and !buildflags to options
  • always use the original pkgdesc and append (mingw32) to the end of pkgdesc
  • always use and follow the original pkgver of the official package
  • always add unset LDFLAGS CPPFLAGS in front of toolchain configure/Makefile generation sequences
  • always put all stuff under the /usr/i486-mingw32 prefix
  • always use any as the architecture
  • always build both shared and static binaries, unless they conflict
  • always remove Win32 executables (*.exe) if the intended package is a library
  • consider using !libtool in options if you get .la files in $pkgdir
  • consider removing unneeded documentation (rm -r $pkgdir/usr/i486-mingw32/share/{doc,info,man})
  • consider explicitly stripping symbols with i486-mingw32-strip in package()
    • if the binary is a DLL, use i486-mingw32-strip -x -g *.dll
    • if the binary is a static lib, use i486-mingw32-strip -g *.a

As mentioned above, the files should all be installed into /usr/i486-mingw32. Specifically, all .dll files should be put into /usr/i486-mingw32/bin as they are dynamic libraries needed at runtime. Their corresponding .dll.a files should go into /usr/i486-mingw32/lib. Please delete any unnecessary documentation and perhaps other files from /usr/share. Cross-compilations packages are not meant for the user but only for the compiler and as such you should try to make them as small as possible.

Always try to match the pkgver in your mingw packages to the pkgver of the corresponding regular packages in the official Arch Linux repos (not the testing repos). This ensures that the cross-compiled software works exactly the same way on mingw without any unexpected bugs. If packages in Arch are out-of-date, there usually is a good reason and you should still follow the Arch version instead of using the most recent upstream release.

Examples

The following examples will try to cover some of the most common conventions and build systems.

Plain Makefile

# Maintainer: yourname <yourmail>
pkgname=mingw32-foo
pkgver=1.0
pkgrel=1
pkgdesc="Foo bar (mingw32)"
arch=('any')
url="http://www.foo.bar"
license=('GPL')
makedepends=('mingw32-gcc')
depends=('mingw32-runtime')
options=('!strip' '!buildflags' 'staticlibs')
source=("http://www.foo.bar/foobar.tar.gz")
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')

build() {
  cd "${srcdir}/foobar"

  unset LDFLAGS

  sed "s|LOCAL = /usr/local|LOCAL = ${pkgdir}/usr/i486-mingw32|g" -i Makefile
  sed "s|CXX = gcc|CXX = i486-mingw32-gcc|g" -i Makefile
  sed "s|CXXFLAGS = |CXXFLAGS = -fPIC |g" -i Makefile

  # or with setconf instead of sed (add 'setconf' to makedepends)
  #setconf Makefile LOCAL "$pkgdir/usr/i486-mingw32"
  #setconf Makefile CXX i486-mingw32-gcc
  #setconf Makefile CXXFLAGS -fPIC

  make
}

package() {
  cd "${srcdir}/foobar"

  mkdir -p "${pkgdir}"/usr/i486-mingw32/{lib,include}
  make install
  chmod 644 "${pkgdir}"/usr/i486-mingw32/lib/libfoo.a
  i486-mingw32-ranlib "${pkgdir}"/usr/i486-mingw32/lib/libfoo.a

  i486-mingw32-strip -x -g "${pkgdir}"/usr/i486-mingw32/bin/*.dll
  i486-mingw32-strip -g "${pkgdir}"/usr/i486-mingw32/lib/*.a
}

Autotools

# Maintainer: yourname <yourmail>
pkgname=mingw32-foo
pkgver=1.0
pkgrel=1
pkgdesc="Foo bar (mingw32)"
arch=('any')
url="http://www.foo.bar"
license=('GPL')
makedepends=('mingw32-gcc')
depends=('mingw32-runtime')
options=('!strip' '!buildflags' 'staticlibs')
source=("http://www.foo.bar/foobar.tar.gz")
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24')

build() {
  cd "${srcdir}/foobar"

  unset LDFLAGS

  ./configure --prefix=/usr/i486-mingw32 --host=i486-mingw32
  make
}

package() {
  cd "${srcdir}/foobar"

  make DESTDIR=${pkgdir} install
  i486-mingw32-strip -x -g "${pkgdir}"/usr/i486-mingw32/bin/*.dll
  i486-mingw32-strip -g "${pkgdir}"/usr/i486-mingw32/lib/*.a
}

CMake

# Maintainer: yourname <yourmail>
pkgname=mingw32-foo
pkgver=1.0
pkgrel=1
pkgdesc="Foo bar (mingw32)"
arch=('any')
url="http://www.foo.bar"
license=('GPL')
makedepends=('mingw32-cmake')
depends=('mingw32-runtime')
options=('!strip' '!buildflags' 'staticlibs')
source=("http://www.foo.bar/foobar.tar.gz"
        "cmake-toolchain.cmake")
md5sums=('4736ac4f34fd9a41fa0197eac23bbc24'
         'dd2b2db48187dff84050fe191d309d81')

build() {
  cd "${srcdir}/foobar-$pkgver"
  unset LDFLAGS
  mkdir build && cd build
  i486-mingw32-cmake \
    -DCMAKE_BUILD_TYPE=Release
    ..
  make
}

package() {
  cd "${srcdir}/foobar-$pkgver"
  cd build
  make DESTDIR="${pkgdir}" install
  i486-mingw32-strip -x -g "${pkgdir}"/usr/i486-mingw32/bin/*.dll
  i486-mingw32-strip -g "${pkgdir}"/usr/i486-mingw32/lib/*.a
}