From 7c594b028829e48fea99523e9f69e46d19cd2ff0 Mon Sep 17 00:00:00 2001
From: Jonas Maebe <jonas@freepascal.org>
Date: Fri, 25 Sep 2015 18:31:54 +0000
Subject: [PATCH]   + added support for using Clang as an assembler, and make
 it the default     for all non-ppc(32/64) Darwin platforms    o pass the
 macosx-version-min/iphoneos-version-min to clang as an assembler,      so
 that it properly sets this information starting with Xcode 7 (solves     
 errors when targeting the iOS simulator, and warnings about object files     
 being compiled for a different OS X version when targeting (Mac) OS X)    o
 the old assembler is still selectable via -Aas-darwin (required with     
 Xcode 3.1.x and older)    o since the first Xcode version that shipped with
 Clang is Xcode 3.2, which      is available for Mac OS X 10.6, most users
 should not encounter any issues      with the new default (in fact, it fixes
 some tests for x86 because Clang      supports some instructions that "as"
 doesn't). Clang does not support      Stabs however, so -gs does require the
 use of -Aas-darwin

git-svn-id: trunk@31830 -
---
 compiler/aarch64/agcpugas.pas | 27 +++++++--------------------
 compiler/arm/agarmgas.pas     | 15 +++++++++++++++
 compiler/assemble.pas         | 12 +++++++++++-
 compiler/systems.inc          |  1 +
 compiler/systems.pas          |  2 +-
 compiler/systems/i_bsd.pas    | 24 ++++++++++++------------
 compiler/x86/agx86att.pas     | 28 ++++++++++++++++++++++++++++
 7 files changed, 75 insertions(+), 34 deletions(-)

diff --git a/compiler/aarch64/agcpugas.pas b/compiler/aarch64/agcpugas.pas
index 37cceb2618..f7adaca746 100644
--- a/compiler/aarch64/agcpugas.pas
+++ b/compiler/aarch64/agcpugas.pas
@@ -45,7 +45,6 @@ unit agcpugas;
 
       TAArch64AppleAssembler=class(TAppleGNUassembler)
         constructor create(info: pasminfo; smart: boolean); override;
-        function MakeCmdLine: TCmdStr; override;
       end;
 
 
@@ -91,18 +90,6 @@ unit agcpugas;
         InstrWriter := TAArch64InstrWriter.create(self);
       end;
 
-    function TAArch64AppleAssembler.MakeCmdLine: TCmdStr;
-      begin
-        { 'as' calls through to clang for aarch64, and that one only supports
-          reading from standard input in case "-" is specified as input file
-          (in which case you also have to specify the language via -x) }
-        result:=inherited;
-{$ifdef hasunix}
-        if DoPipe then
-          result:=result+' -x assembler -'
-{$endif}
-      end;
-
 
 {****************************************************************************}
 {                  Helper routines for Instruction Writer                    }
@@ -295,14 +282,14 @@ unit agcpugas;
             dollarsign: '$';
           );
 
-       as_aarch64_gas_darwin_info : tasminfo =
+       as_aarch64_clang_darwin_info : tasminfo =
           (
-            id     : as_darwin;
-            idtxt  : 'AS-DARWIN';
-            asmbin : 'as';
-            asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch arm64';
+            id     : as_clang;
+            idtxt  : 'CLANG';
+            asmbin : 'clang';
+            asmcmd : '-c -o $OBJ $EXTRAOPT -arch arm64 $DARWINVERSION -x assembler $ASM';
             supported_targets : [system_aarch64_darwin];
-            flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
+            flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
             labelprefix : 'L';
             comment : '# ';
             dollarsign: '$';
@@ -311,5 +298,5 @@ unit agcpugas;
 
 begin
   RegisterAssembler(as_aarch64_gas_info,TAArch64Assembler);
-  RegisterAssembler(as_aarch64_gas_darwin_info,TAArch64AppleAssembler);
+  RegisterAssembler(as_aarch64_clang_darwin_info,TAArch64AppleAssembler);
 end.
diff --git a/compiler/arm/agarmgas.pas b/compiler/arm/agarmgas.pas
index 4f2f92161c..687a1c7a3f 100644
--- a/compiler/arm/agarmgas.pas
+++ b/compiler/arm/agarmgas.pas
@@ -425,7 +425,22 @@ unit agarmgas;
           );
 
 
+       as_arm_clang_darwin_info : tasminfo =
+          (
+            id     : as_clang;
+            idtxt  : 'CLANG';
+            asmbin : 'clang';
+            asmcmd : '-c -o $OBJ $EXTRAOPT -arch $ARCH $DARWINVERSION -x assembler $ASM';
+            supported_targets : [system_arm_darwin];
+            flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
+            labelprefix : 'L';
+            comment : '# ';
+            dollarsign: '$';
+          );
+
+
 begin
   RegisterAssembler(as_arm_gas_info,TARMGNUAssembler);
   RegisterAssembler(as_arm_gas_darwin_info,TArmAppleGNUAssembler);
+  RegisterAssembler(as_arm_clang_darwin_info,TArmAppleGNUAssembler);
 end.
diff --git a/compiler/assemble.pas b/compiler/assemble.pas
index 1b9cf3766d..c4b2e07cd0 100644
--- a/compiler/assemble.pas
+++ b/compiler/assemble.pas
@@ -898,6 +898,13 @@ Implementation
     function TExternalAssembler.MakeCmdLine: TCmdStr;
       begin
         result:=asminfo^.asmcmd;
+        { for Xcode 7.x and later }
+        if MacOSXVersionMin<>'' then
+          Replace(result,'$DARWINVERSION','-mmacosx-version-min='+MacOSXVersionMin)
+        else if iPhoneOSVersionMin<>'' then
+          Replace(result,'$DARWINVERSION','-miphoneos-version-min='+iPhoneOSVersionMin)
+        else
+          Replace(result,'$DARWINVERSION','');
 {$ifdef arm}
         if (target_info.system=system_arm_darwin) then
           Replace(result,'$ARCH',lower(cputypestr[current_settings.cputype]));
@@ -911,7 +918,10 @@ Implementation
          begin
 {$ifdef hasunix}
           if DoPipe then
-            Replace(result,'$ASM','')
+            if asminfo^.id<>as_clang then
+              Replace(result,'$ASM','')
+            else
+              Replace(result,'$ASM','-')
           else
 {$endif}
              Replace(result,'$ASM',maybequoted(AsmFileName));
diff --git a/compiler/systems.inc b/compiler/systems.inc
index bd7ea8a090..d834446466 100644
--- a/compiler/systems.inc
+++ b/compiler/systems.inc
@@ -220,6 +220,7 @@
              ,as_arm_elf32
              ,as_i8086_omf
              ,as_llvm
+             ,as_clang
        );
 
        tlink = (ld_none,
diff --git a/compiler/systems.pas b/compiler/systems.pas
index 78625d8033..423e3df1ed 100644
--- a/compiler/systems.pas
+++ b/compiler/systems.pas
@@ -70,7 +70,7 @@ interface
           id          : tasm;
           idtxt       : string[12];
           asmbin      : string[8];
-          asmcmd      : string[50];
+          asmcmd      : string[70];
           supported_targets : set of tsystem;
           flags        : set of tasmflags;
           labelprefix : string[3];
diff --git a/compiler/systems/i_bsd.pas b/compiler/systems/i_bsd.pas
index 05aa56e6ab..6cce4dbbad 100644
--- a/compiler/systems/i_bsd.pas
+++ b/compiler/systems/i_bsd.pas
@@ -741,8 +741,8 @@ unit i_bsd;
             Cprefix      : '_';
             newline      : #10;
             dirsep       : '/';
-            assem        : as_darwin;
-            assemextern  : as_darwin;
+            assem        : as_clang;
+            assemextern  : as_clang;
             link         : ld_none;
             linkextern   : ld_bsd;
             ar           : ar_gnu_ar;
@@ -806,8 +806,8 @@ unit i_bsd;
             Cprefix      : '_';
             newline      : #10;
             dirsep       : '/';
-            assem        : as_darwin;
-            assemextern  : as_darwin;
+            assem        : as_clang;
+            assemextern  : as_clang;
             link         : ld_none;
             linkextern   : ld_bsd;
             ar           : ar_gnu_ar;
@@ -936,8 +936,8 @@ unit i_bsd;
             Cprefix      : '_';
             newline      : #10;
             dirsep       : '/';
-            assem        : as_darwin;
-            assemextern  : as_darwin;
+            assem        : as_clang;
+            assemextern  : as_clang;
             link         : ld_none;
             linkextern   : ld_bsd;
             ar           : ar_gnu_ar;
@@ -1000,8 +1000,8 @@ unit i_bsd;
             Cprefix      : '_';
             newline      : #10;
             dirsep       : '/';
-            assem        : as_darwin;
-            assemextern  : as_darwin;
+            assem        : as_clang;
+            assemextern  : as_clang;
             link         : ld_none;
             linkextern   : ld_bsd;
             ar           : ar_gnu_ar;
@@ -1064,8 +1064,8 @@ unit i_bsd;
             Cprefix      : '_';
             newline      : #10;
             dirsep       : '/';
-            assem        : as_darwin;
-            assemextern  : as_darwin;
+            assem        : as_clang;
+            assemextern  : as_clang;
             link         : ld_none;
             linkextern   : ld_bsd;
             ar           : ar_gnu_ar;
@@ -1129,8 +1129,8 @@ unit i_bsd;
             Cprefix      : '_';
             newline      : #10;
             dirsep       : '/';
-            assem        : as_darwin;
-            assemextern  : as_darwin;
+            assem        : as_clang;
+            assemextern  : as_clang;
             link         : ld_none;
             linkextern   : ld_bsd;
             ar           : ar_gnu_ar;
diff --git a/compiler/x86/agx86att.pas b/compiler/x86/agx86att.pas
index 8dee573a45..9c43ebdb36 100644
--- a/compiler/x86/agx86att.pas
+++ b/compiler/x86/agx86att.pas
@@ -484,6 +484,19 @@ interface
             dollarsign: '$';
           );
 
+       as_x86_64_clang_darwin_info : tasminfo =
+          (
+            id     : as_clang;
+            idtxt  : 'CLANG';
+            asmbin : 'clang';
+            asmcmd : '-c -o $OBJ $EXTRAOPT -arch x86_64 $DARWINVERSION -x assembler $ASM';
+            supported_targets : [system_x86_64_darwin,system_x86_64_iphonesim];
+            flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
+            labelprefix : 'L';
+            comment : '# ';
+            dollarsign: '$';
+          );
+
 {$else x86_64}
        as_i386_as_info : tasminfo =
           (
@@ -545,6 +558,19 @@ interface
             dollarsign: '$';
           );
 
+       as_i386_clang_darwin_info : tasminfo =
+          (
+            id     : as_clang;
+            idtxt  : 'CLANG';
+            asmbin : 'clang';
+            asmcmd : '-c -o $OBJ $EXTRAOPT -arch i386 $DARWINVERSION -x assembler $ASM';
+            supported_targets : [system_i386_darwin,system_i386_iphonesim];
+            flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
+            labelprefix : 'L';
+            comment : '# ';
+            dollarsign: '$';
+          );
+
        as_i386_gas_info : tasminfo =
           (
             id     : as_ggas;
@@ -568,11 +594,13 @@ initialization
   RegisterAssembler(as_x86_64_yasm_info,Tx86ATTAssembler);
   RegisterAssembler(as_x86_64_gas_info,Tx86ATTAssembler);
   RegisterAssembler(as_x86_64_gas_darwin_info,Tx86AppleGNUAssembler);
+  RegisterAssembler(as_x86_64_clang_darwin_info,Tx86AppleGNUAssembler);
 {$else x86_64}
   RegisterAssembler(as_i386_as_info,Tx86ATTAssembler);
   RegisterAssembler(as_i386_gas_info,Tx86ATTAssembler);
   RegisterAssembler(as_i386_yasm_info,Tx86ATTAssembler);
   RegisterAssembler(as_i386_gas_darwin_info,Tx86AppleGNUAssembler);
+  RegisterAssembler(as_i386_clang_darwin_info,Tx86AppleGNUAssembler);
   RegisterAssembler(as_i386_as_aout_info,Tx86AoutGNUAssembler);
 {$endif x86_64}
 end.