Skip to content

Get hello world compiling for iOS #5145

Open

Description

@tanis2000

I'm just figuring out at what point in time you are with iOS support for Zig. I tried to compile the basic project that you get when you initialize a new Zig project with the following command:

zig build-exe ./src/main.zig -target aarch64-ios

And I get the following error:

/usr/local/Cellar/zig/0.6.0_1/lib/zig/std/debug.zig:662:21: error: openSelfDebugInfo unsupported for this platform
            else => @compileError("openSelfDebugInfo unsupported for this platform"),
                    ^

Does that mean that iOS is not supported at all or is there anything I could do to help adding support for that platform?

Activity

  1. Sobeston commented on Apr 23, 2020

    @Sobeston
    Contributor

    Compiling a debug build (the default) has safety features enabled, and many of these things are OS specific. If you want to make good use of zig's runtime safety features, someone would have to implement these for IOS.

    Large areas of the standard library as a whole likely won't be available yet.

    For now, I think IOS will work in release fast and release small (providing you can avoid using a lot of the standard library).

  2. tanis2000 commented on Apr 27, 2020

    @tanis2000
    Author

    Both release-fast and release-small seem to fail with the same error. Am I doing something wrong?

    ➜  test-zig zig build-exe ./src/main.zig -target aarch64-ios --release-fast
    /usr/local/Cellar/zig/0.6.0_1/lib/zig/std/debug.zig:662:21: error: openSelfDebugInfo unsupported for this platform
                else => @compileError("openSelfDebugInfo unsupported for this platform"),
                        ^
    ➜  test-zig zig build-exe ./src/main.zig -target aarch64-ios --release-small
    /usr/local/Cellar/zig/0.6.0_1/lib/zig/std/debug.zig:662:21: error: openSelfDebugInfo unsupported for this platform
                else => @compileError("openSelfDebugInfo unsupported for this platform"),
                        ^
    
  3. LemonBoy commented on Apr 27, 2020

    @LemonBoy
    Contributor

    Am I doing something wrong?

    The --release-* switches won't do anything for your problem. Try adding .ios in the switch where the @compilError is, I believe the Darwin codepath works just fine on IOS too.

  4. tanis2000 commented on Apr 27, 2020

    @tanis2000
    Author

    So far my src/main.zig file (the only one I have besides the build.zig) is the following:

    const std = @import("std");
    
    pub fn main() anyerror!void {
        std.debug.warn("All your codebase are belong to us.\n", .{});
    }
    

    I even tried this because I thought std.debug could be the issue:

    const std = @import("std");
    
    pub fn main() anyerror!void {
        const stdout = std.io.getStdOut().outStream();
        try stdout.print("Hello, {}!\n", .{"world"});
    }
    

    but the error is still the same.
    I don't get where I should add an .ios to a switch... which switch? :)

  5. daurnimator commented on Apr 27, 2020

    @daurnimator
    Contributor

    The --release-* switches won't do anything for your problem. Try adding .ios in the switch where the @compilError is, I believe the Darwin codepath works just fine on IOS too.

    wait..... why is ios different to darwin? they're the same kernel

  6. tanis2000 commented on Apr 27, 2020

    @tanis2000
    Author

    @daurnimator yes it is the same kernel. But if I add .ios to this switch in the std library (I believe this is the switch @LemonBoy was referring to) then it compiles:

    /// TODO resources https://github.com/ziglang/zig/issues/4353
    pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
        noasync {
            if (builtin.strip_debug_info)
                return error.MissingDebugInfo;
            if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {
                return root.os.debug.openSelfDebugInfo(allocator);
            }
            switch (builtin.os.tag) {
                .linux,
                .freebsd,
                .netbsd,
                .dragonfly,
                .macosx,
                .windows,
                .ios,
                => return DebugInfo.init(allocator),
                else => @compileError("openSelfDebugInfo unsupported for this platform"),
            }
        }
    }
    
  7. LemonBoy commented on Apr 27, 2020

    @LemonBoy
    Contributor

    I don't get where I should add an .ios to a switch... which switch? :)

    The one above /usr/local/Cellar/zig/0.6.0_1/lib/zig/std/debug.zig:662. I'd suggest you to fetch a copy from git to avoid modifying the homebrew copy.

    wait..... why is ios different to darwin? they're the same kernel

    I forgot to put back the .ios, .watchos and friends when I refactored the debug stuff.

  8. tanis2000 commented on Apr 27, 2020

    @tanis2000
    Author

    @LemonBoy hah! now that's clear :)

    Ok, then I have one more question regarding compiling for iOS. Let's assume that:

    • I have a C library that I wrote that I use to make games on all kind of platforms with.
    • In that library I am including SDL2 and other libraries as source code and compile them all statically into my library to make sure that I'm using the same versions that I have written my code with.
    • I believe that Zig has no clue about CMake files
    • But Zig is using clang to compile C code, so I guess it should be able to understand Obj-C code, too

    Would it be possible to provide to Zig a list of folders with the source code of SDL2 and the like and write a build.zig script that compiles all of them with the correct inclusion files and the correct switches for clang and also for the Obj-C files?

  9. prime31 commented on May 3, 2020

    @prime31
    Contributor

    @tanis2000 you're prolly gonna need to dive into this, update SDL and add iOS to the build script: https://github.com/andrewrk/zig-sdl

  10. tanis2000 commented on May 4, 2020

    @tanis2000
    Author

    @prime31 I already did all of that in my own repo without all the extra stuff of that SDL repo. But I still have issues compiling for macOS, not even iOS... Actually the compiler itself is crashing: #5188

  11. added this to the 0.8.0 milestone on May 30, 2020
  12. added
    contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.
    enhancementSolving this issue will likely involve adding new logic or components to the codebase.
    on May 30, 2020
  13. changed the title [-]Compiling for iOS[/-] [+]Get hello world compiling for iOS[/+] on May 30, 2020
  14. andrewrk commented on May 30, 2020

    @andrewrk
    Member

    Would it be possible to provide to Zig a list of folders with the source code of SDL2 and the like and write a build.zig script that compiles all of them with the correct inclusion files and the correct switches for clang and also for the Obj-C files?

    Yes, and you can see a proof-of-concept here: https://github.com/andrewrk/zig-sdl

  15. 13 remaining items

  16. coolbluewater commented on Feb 3, 2021

    @coolbluewater

    @SebastianKeller,
    Running the steps you listed produces
    dyld: Symbol not found: __tlv_bootstrap

    Any idea what might be causing this? I'm running zig 0.7.1 and the simulator is iOS 14.3.

  17. g-w1 commented on Feb 3, 2021

    @g-w1
    Contributor

    The symbol is defined on master, not 0.7.1
    image

  18. coolbluewater commented on Feb 3, 2021

    @coolbluewater

    @g-w1, Thanks that would explain it. As a zig newbie, are builds of master available to install?
    If not, I'll compile from source, a good way to learn the system.

  19. g-w1 commented on Feb 3, 2021

    @g-w1
    Contributor
  20. coolbluewater commented on Feb 3, 2021

    @coolbluewater

    @g-w1, @SebastianKeller - thanks all. Will report back.

  21. coolbluewater commented on Feb 3, 2021

    @coolbluewater

    @SebastianKeller, running xcrun simctl spawn works, and the output is in Terminal.

    But running the .app bundle doesn't seem to produce any output in Console. Investigating.

  22. coolbluewater commented on Feb 3, 2021

    @coolbluewater

    Update: while I'm still unable to find where the "Hello, world" got written to, adding this worked:

    std.os.exit(1);

    Now Console shows Service exited with abnormal code: 1
    So that's probably the first iOS app written entirely in zig with no Objective-C or Swift! :)

  23. mikdusan commented on Feb 3, 2021

    @mikdusan
    Member

    hmm... don't app launchers close file handles 0,1,2 and force use of logging api ... and console.app can read?

  24. coolbluewater commented on Feb 4, 2021

    @coolbluewater

    @mikdusan, you're probably correct. Now trying to use os_log.

    What is the zig equivalent of Objective-C's#import <os/log.h>? This allows framework headers to be imported easily.

    I tried const log = @import("os/log.h"); but that fails with or without the .h.

    Edit: Think I may need to create an objective-C library with that exposes functionality to zig via the C ABI. Also opened #7944 to discuss whether Objective-C frameworks can be supported more directly in zig.

  25. g-w1 commented on Feb 4, 2021

    @g-w1
    Contributor

    If you want to include the c library you probably want

    const c = @cImport({
    @cInclude("os/log.h");
    });
  26. coolbluewater commented on Feb 4, 2021

    @coolbluewater

    @g-w1, unfortunately that doesn't work.
    More detail: Apple frameworks have a known location and layout that clang uses to resolve #import <os/log.h> when parsing a source file as objective-c, but not as plain C. Since zig uses clang in plain-C mode to parse files, this mechanism isn't invoked and so the header can't be located. That's why I've opened the issue mentioned above to discuss the future of objective-C interop in zig.

  27. kerrhome commented on Feb 17, 2021

    @kerrhome

    @coolbluewater my buddy Bill just did some writing to the iOS console yesterday for us testing a Zig build (using the iOS SDK still). He used os_log(), which you should be able to create an extern for _os_log_internal to using this within your Zig app without needing the full SDK to build against. It was extremely helpful in our debugging yesterday (which we have working beautifully now). Man, Zig is great.

  28. modified the milestones: 0.9.0, 0.10.0 on May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions