[m-rev.] for review: Initial support for cross-compiling using clang.

Peter Wang novalazy at gmail.com
Mon Jul 18 16:17:29 AEST 2022


[I will rename README.cross to README.cross-gcc in a separate change.]
---

README.cross-clang:
.nocopyright:
    Add instructions for cross-compiling using clang.

README.cross:
    Clarify this document discusses cross-compilation using gcc.

README.md:
    Add reference to new readme.

tools/configure_cross:
    Adapt script to support a FreeBSD x86-64 target using clang.

NEWS:
    Announce change to script.

diff --git a/.nocopyright b/.nocopyright
index c51cff9d1..41b243dab 100644
--- a/.nocopyright
+++ b/.nocopyright
@@ -38,3 +38,4 @@ README.ssdebug
 README.CSharp
 README.clang
 README.cross
+README.cross-clang
diff --git a/NEWS b/NEWS
index 3732d41f6..a88626820 100644
--- a/NEWS
+++ b/NEWS
@@ -408,6 +408,12 @@ Changes to the Mercury compiler
 * The deprecated option `--trail-segments` has been deleted and the grade
   component `trseg` is no longer accepted as a synonym for `tr`.
 
+Portability improvements
+------------------------
+
+* We have updated the script `tools/configure_cross` to support
+  cross-compiling using clang.
+
 Changes to the extras distribution
 ----------------------------------
 
diff --git a/README.cross b/README.cross
index c53b1c565..aac597b64 100644
--- a/README.cross
+++ b/README.cross
@@ -1,8 +1,8 @@
 -----------------------------------------------------------------------------
 
-COMPILING MERCURY WITH A CROSS-COMPILER
+CROSS-COMPILING MERCURY WITH GCC
 
-You can build the Mercury system with a C cross-compiler so that the Mercury
+You can build the Mercury system with a GCC cross-compiler so that the Mercury
 installation will run on a different platform from your host system.
 The following targets are currently supported:
 
diff --git a/README.cross-clang b/README.cross-clang
new file mode 100644
index 000000000..8291b5a56
--- /dev/null
+++ b/README.cross-clang
@@ -0,0 +1,96 @@
+-----------------------------------------------------------------------------
+
+CROSS-COMPILING MERCURY WITH CLANG
+
+You can cross-compile the Mercury system for a different target architecture
+from the host system using clang. The following targets are currently
+supported:
+
+  * FreeBSD x86-64
+
+Furthermore, instead of transferring the Mercury installation to the target
+system and running it there, you can augment the cross-compiled installation
+with files from a native Mercury installation, allowing you to cross-compile
+Mercury programs for the target system on the host system.
+
+-----------------------------------------------------------------------------
+
+INSTRUCTIONS
+
+ 1. Install Mercury for the host system as usual using clang.
+
+ 2. Obtain a "sysroot" file system for your target system, that is,
+    a logical root directory in which compiler will search for headers and
+    libraries.
+
+    For FreeBSD, you can copy these directories from a running system:
+
+        /lib /usr/include /usr/lib /usr/local/include /usr/local/lib
+
+    Place the sysroot somewhere, e.g. ~/sysroots/x86_64-unknown-freebsd13.0
+
+ 3. Set the CC environment variable as follows:
+
+        CC="clang -target TRIPLE --sysroot=SYSROOT-PATH"
+        export CC
+
+    where TRIPLE is the triple that specifies the target architecture
+    (e.g. x86_64-unknown-freebsd13.0) and SYSROOT-PATH points to the sysroot
+    directory.
+
+    It may be convenient to create a shell script that invokes clang with those
+    options; then you can set CC to refer to that shell script.
+
+ 4. Unpack a fresh copy of the Mercury source tree.
+    Now, instead of running ./configure, run:
+
+        tools/configure_cross --host=TRIPLE [--with-cc=COMMAND] \
+            <other configure arguments>
+
+    The `--host` option is required. TRIPLE is target triple as above.
+
+    The `--with-cc` option can be used to specify the clang command instead of
+    setting the CC environment variable as described above.
+
+    Any later options are passed through to the configure script.
+    A call to the `configure_cross_clang` script might look like:
+
+        tools/configure_cross \
+            --host=x86_64-unknown-freebsd13.0 \
+            --prefix=/usr/local/mercury-x86_64-unknown-freebsd13.0 \
+            --enable-libgrades=hlc.gc
+
+ 5. Now you can install Mercury as usual, e.g.
+
+        mmake depend
+        mmake
+        mmake install
+
+-----------------------------------------------------------------------------
+
+ADAPTING THE MERCURY INSTALLATION FOR USE ON THE HOST SYSTEM
+
+To use the cross-compiled Mercury installation on the host system,
+you need to copy executables from a native Mercury installation's `bin`
+directory to the cross-compiled Mercury installation's `bin` directory.
+This can be done using the `tools/copy_mercury_binaries` script,
+which is called like this:
+
+    tools/copy_mercury_binaries SRC DEST
+
+where SRC is the path to the native Mercury installation,
+and DEST is the path to the cross-compiled Mercury installation.
+The versions of the two Mercury installations should be the same,
+or at least very similar.
+
+Once that is done, you can use the `mmc` script from the cross-compiled Mercury
+installation to compile Mercury programs for the target system, e.g.
+
+    % /usr/local/mercury-x86_64-unknown-freebsd13.0/bin/mmc -m hello
+    Making Mercury/int3s/hello.int3
+    Making Mercury/ints/hello.int
+    Making Mercury/cs/hello.c
+    Making Mercury/os/hello.o
+    Making hello
+
+-----------------------------------------------------------------------------
diff --git a/README.md b/README.md
index eed03bd63..7a876c66b 100644
--- a/README.md
+++ b/README.md
@@ -54,7 +54,8 @@ Specific information is contained in individual README files:
         [Cygwin](README.Cygwin))
 
   * Other platform information
-      * [Cross compilation](README.cross)
+      * [Cross compilation using gcc](README.cross)
+      * [Cross compilation using clang](README.cross-clang)
       * [Docker](README.Docker)
       * [x86](README.x86)
 
diff --git a/tools/configure_cross b/tools/configure_cross
index eab85b502..c3513c600 100755
--- a/tools/configure_cross
+++ b/tools/configure_cross
@@ -2,7 +2,7 @@
 # vim: ft=sh ts=4 sw=4 et
 #---------------------------------------------------------------------------#
 # Copyright (C) 2012 The University of Melbourne.
-# Copyright (C) 2014, 2018, 2021 The Mercury team.
+# Copyright (C) 2014, 2018, 2021-2022 The Mercury team.
 # This file may only be copied under the terms of the GNU General
 # Public License - see the file COPYING in the Mercury distribution.
 #---------------------------------------------------------------------------#
@@ -40,13 +40,22 @@ then
     exit 1
 fi
 
-hostcc=${hostcc:-"${host}-gcc"}
-
-if command -v "$hostcc" >/dev/null
+hostcc=${hostcc:-${CC:-}}
+if test -z "$hostcc"
 then
-    true
+    # If no C compiler is specified, then assume we want to use a gcc
+    # cross-compiler.
+    hostcc="${host}-gcc"
+fi
+
+if $hostcc -v 2>&1 | grep -q '^clang version '
+then
+    cc_type=clang
+elif $hostcc -v 2>&1 | grep -q '^gcc version '
+then
+    cc_type=gcc
 else
-    echo "You need $hostcc in your PATH."
+    echo "You need to specify a C compiler."
     exit 1
 fi
 
@@ -70,8 +79,8 @@ then
 fi
 
 # Set values which would otherwise be determined with AC_TRY_RUN.
-case $host in
-    i686-*-mingw32* | x86_64-*-mingw32*)
+case "$cc_type:$host" in
+    gcc:i686-*-mingw32* | gcc:x86_64-*-mingw32*)
         # Taken from the config.cache file after running configure -C in msys.
         mercury_cv_cc_type=gcc
         mercury_cv_siginfo_t=no
@@ -86,7 +95,7 @@ case $host in
         mercury_cv_gcc_model_reg=yes
         mercury_cv_cannot_use_structure_assignment=yes
         ;;
-    aarch64-linux-gnu | aarch64-linux-musl)
+    gcc:aarch64-linux-gnu | gcc:aarch64-linux-musl)
         # Taken from the config.cache file after running configure -C
         # - in a Debian 10 arm64 environment (for glibc)
         # - in a Alpine Linux aarch64 environment (for musl)
@@ -103,6 +112,22 @@ case $host in
         mercury_cv_gcc_model_reg=yes
         mercury_cv_cannot_use_structure_assignment=no
         ;;
+    clang:x86_64-*freebsd*)
+        # Taken from the config.cache file after running configure -C in
+        # FreeBSD 13.0 x86-64.
+        mercury_cv_cc_type=clang
+        mercury_cv_siginfo_t=yes
+        mercury_cv_pc_access=no
+        mercury_cv_is_bigender=no
+        mercury_cv_is_littleender=yes
+        mercury_cv_normal_system_retval=yes
+        mercury_cv_can_do_pending_io=yes
+        mercury_cv_gcc_labels=no
+        mercury_cv_asm_labels=no
+        mercury_cv_gcc_model_fast=no
+        mercury_cv_gcc_model_reg=no
+        mercury_cv_cannot_use_structure_assignment=yes
+        ;;
     *)
         echo "unknown host: $host" >&2
         exit 1
-- 
2.36.1



More information about the reviews mailing list