Error building C package with Swift 5.3 compiler

Any idea why this Swift package SwiftyHaru has stopped building with the Swift 5.3 beta toolchain from Xcode?

With the Swift 5.2 toolchain:

MacBook-Pro:SwiftyHaru pvieito$ swift --version                                                
Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
Target: x86_64-apple-darwin20.0.0
MacBook-Pro:SwiftyHaru pvieito$ swift build
[103/103] Merging module SwiftyHaru

With the new Swift 5.3 beta toolchain:

MacBook-Pro:SwiftyHaru pvieito$ swift --version
Apple Swift version 5.3 (swiftlang-1200.0.25.2 clang-1200.0.27.1)
Target: x86_64-apple-darwin20.0.0
MacBook-Pro:SwiftyHaru pvieito$ swift build    
While building module 'CLibPNG' imported from /Users/pvieito/Downloads/SwiftyHaru/CLibHaru/hpdf_image_png.c:23:
In file included from <module-includes>:1:
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:151:4: error: unknown type name 'png_error_ptr'
   png_error_ptr error_fn;    /* function for printing errors and aborting */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:155:4: error: unknown type name 'png_voidp'
   png_voidp error_ptr;       /* user supplied struct for error functions */
   ^
unknown type name 'png_rw_ptr'
   png_rw_ptr write_data_fn;  /* function for writing output data */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:157:4: error: unknown type name 'png_rw_ptr'
   png_rw_ptr read_data_fn;   /* function for reading input data */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:158:4: error: unknown type name 'png_voidp'
   png_voidp io_ptr;          /* ptr to application struct for I/O functions */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:178:4: error: unknown type name 'png_uint_32'
   png_uint_32 mode;          /* tells us where we are in the PNG file */
   ^
:4: error: unknown type name 'png_uint_32'
   png_uint_32 flags;         /* flags indicating various things to libpng */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:180:4: error: unknown type name 'png_uint_32'
   png_uint_32 transformations; /* which transformations to perform */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:182:4: error: unknown type name 'png_uint_32'
   png_uint_32 zowner;        /* ID (chunk type) of zstream owner, 0 if none */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:213:4: error: unknown type name 'png_uint_32'
   png_uint_32 width;         /* width of image in pixels */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:214:4: error: unknown type name 'png_uint_32'
   png_uint_32 height;        /* height of image in pixels */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:215:4: error: unknown type name 'png_uint_32'
   png_uint_32 num_rows;      /* number of rows in current pass */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:216:4: error: unknown type name 'png_uint_32'
   png_uint_32 usr_width;     /* width of row at start of write */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:217:4: error: unknown type name 'png_size_t'; did you mean 'z_size_t'?
   png_size_t rowbytes;       /* size of row in bytes */


/Applications/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/usr/include/zconf.h:248:21: note: 'z_size_t' declared here
     typedef size_t z_size_t;
                    ^
While building module 'CLibPNG' imported from /Users/pvieito/Downloads/SwiftyHaru/CLibHaru/hpdf_image_png.c:23:
In file included from <module-includes>:1:
 error: unknown type name 'png_uint_32'
   png_uint_32 iwidth;        /* width of current interlaced row in pixels */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:219:4: error: unknown type name 'png_uint_32'
   png_uint_32 row_number;    /* current row in interlace pass */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:220:4: error: unknown type name 'png_uint_32'
   png_uint_32 chunk_name;    /* PNG_CHUNK() id of current chunk */
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:221:4: error: unknown type name 'png_bytep'
   png_bytep prev_row;        /* buffer to save previous (unfiltered) row.
   ^
/Users/pvieito/Downloads/SwiftyHaru/CLibPNG/include/pngstruct.h:226:4: error: unknown type name 'png_bytep'
   png_bytep row_buf;         /* buffer to save current (unfiltered) row.
   ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
/Users/pvieito/Downloads/SwiftyHaru/CLibHaru/hpdf_image_png.c:23:10: fatal error: could not build module 'CLibPNG'
#include "png.h"
 ~~~~~~~~^~~~~~~
21 errors generated.
[43/72] Compiling CLibHaru hpdf_fontdef_tt.c

I can reproduce on Ubuntu 20.04 with the official 5.2.5 release working and the July 20 official 5.3 snapshot build failing in the same way, once I patched one of your files for linux:

diff --git a/SwiftyHaru/DefaultStringConvertible.swift b/SwiftyHaru/DefaultStringConvertible.swift
index a165258..639b639 100644
--- a/SwiftyHaru/DefaultStringConvertible.swift
+++ b/SwiftyHaru/DefaultStringConvertible.swift
@@ -223,11 +223,11 @@ private func generateDeepDescription(_ any: Any) -> String {
     extension String {
 
         public func hasPrefix(_ prefix: String) -> Bool {
-            return prefix == String(characters.prefix(prefix.characters.count))
+            return prefix == String(self.prefix(prefix.count))
         }
         
         public func hasSuffix(_ suffix: String) -> Bool {
-            return suffix == String(characters.suffix(suffix.characters.count))
+            return suffix == String(self.suffix(suffix.count))
         }
     }
 #endif

The way to debug this is to pass -v -j 1 to the package manager, to show all the commands it's running and to run them sequentially, so the error output comes right after the failing command. Doing that, I see no difference between the flags passed to the failing clang command and the working one, but there was a big jump from clang 7.0 with Swift 5.2 to clang 10.0 with Swift 5.3, so clang is behaving differently.

It likely has to do with module maps, as the failing clang command still has that <module-includes> line:

/home/butta/swift-5.3-DEVELOPMENT-SNAPSHOT-2020-07-20-a-ubuntu18.04/usr/bin/clang -target x86_64-unknown-linux-gnu -fPIC -g -O0 -DSWIFT_PACKAGE=1 -DDEBUG=1 -fblocks -fmodules -fmodule-name=CLibHaru -I /home/butta/SwiftyHaru/CLibHaru/include -I /home/butta/SwiftyHaru/CLibPNG/include -fmodule-map-file=/home/butta/SwiftyHaru/.build/x86_64-unknown-linux-gnu/debug/CLibPNG.build/module.modulemap -fmodules-cache-path=/home/butta/SwiftyHaru/.build/x86_64-unknown-linux-gnu/debug/ModuleCache -MD -MT dependencies -MF /home/butta/SwiftyHaru/.build/x86_64-unknown-linux-gnu/debug/CLibHaru.build/hpdf_image_png.c.d -c /home/butta/SwiftyHaru/CLibHaru/hpdf_image_png.c -o /home/butta/SwiftyHaru/.build/x86_64-unknown-linux-gnu/debug/CLibHaru.build/hpdf_image_png.c.o
While building module 'CLibPNG' imported from /home/butta/SwiftyHaru/CLibHaru/hpdf_image_png.c:23:
In file included from <module-includes>:1:
/home/butta/SwiftyHaru/CLibPNG/include/pngstruct.h:151:4: error: unknown type name 'png_error_ptr'
    png_error_ptr error_fn;    /* function for printing errors and aborting */

Anders has been submitting a bunch of SPM pulls having to do with module maps recently, maybe one of them is related to your problem. If not, you should definitely open an issue on the Swift JIRA at bugs.swift.org.

1 Like