Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve translate-c for libcurl simple example #988

Closed
ghost opened this issue May 5, 2018 · 7 comments · Fixed by #4040
Closed

improve translate-c for libcurl simple example #988

ghost opened this issue May 5, 2018 · 7 comments · Fixed by #4040
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@ghost
Copy link

ghost commented May 5, 2018

For example, with C you can do this:

https://github.com/curl/curl/blob/master/docs/examples/simple.c

and other languages:

http://rosettacode.org/wiki/HTTP

@andrewrk
Copy link
Member

andrewrk commented May 5, 2018

You can use libcurl in zig too.

I wonder how far this will get you:

zig translate-c simple.c

@ghost
Copy link
Author

ghost commented May 5, 2018

not very far :(

$ zig translate-c \
--libc-include-dir 'C:\cygwin64\usr\x86_64-w64-mingw32\sys-root\mingw\include' \
simple.c
unexpected error from clang: too many errors emitted, stopping now
D:\Desktop\zig-0.2.0\lib\zig\include\mmintrin.h:81:40: error: passing '__v2si'
(aka 'int') to parameter of incompatible type
'__attribute__((__vector_size__(2 * sizeof(int)))) int' (vector of 2 'int' values)
    return __builtin_ia32_vec_ext_v2si((__v2si)__m, 0);
                                       ^

@andrewrk
Copy link
Member

andrewrk commented May 7, 2018

this issue is a duplicate of #910

but I'm going to repurpose it to be an issue to improve translate-c of this curl example.

@andrewrk andrewrk added this to the 0.4.0 milestone May 7, 2018
@andrewrk andrewrk changed the title Download a file improve translate-c for libcurl simple example May 7, 2018
@andrewrk

This comment has been minimized.

@andrewrk andrewrk modified the milestones: 0.4.0, 0.5.0 Feb 7, 2019
@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Jun 16, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Aug 21, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Oct 23, 2019
@ziglang ziglang deleted a comment Nov 1, 2019
@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Jan 2, 2020
@daurnimator
Copy link
Contributor

The curl sample file now fails with:

/home/daurnimator/src/zig/988.zig:2393:36: error: expected type 'enum_unnamed_33', found 'comptime_int'
        _ = curl_easy_setopt(curl, @as(CURLoption, CURLOPT_URL), "https://example.com");
                                   ^
/home/daurnimator/src/zig/988.zig:1414:25: note: enum_unnamed_33 declared here
const enum_unnamed_33 = extern enum {
                        ^
/home/daurnimator/src/zig/lib/std/start.zig:258:17: error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'
                @compileError(bad_main_ret);
                ^

@andrewrk
Copy link
Member

andrewrk commented Jan 2, 2020

The example above now translates to:

pub export fn main() c_int {
    var curl: ?*CURL = undefined;
    var res: CURLcode = undefined;
    curl = curl_easy_init();
    if (curl != null) {
        _ = curl_easy_setopt(curl, @intToEnum(CURLoption, CURLOPT_URL), "https://example.com");
        _ = curl_easy_setopt(curl, @intToEnum(CURLoption, CURLOPT_FOLLOWLOCATION), @as(c_long, 1));
        res = curl_easy_perform(curl);
        if (@enumToInt(res) != @bitCast(c_uint, CURLE_OK)) _ = fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        curl_easy_cleanup(curl);
    }
    return 0;
}

It would be nice if Zig could figure out to improve the enum handling in this way instead:

pub export fn main() c_int {
    var curl: ?*CURL = undefined;
    var res: CURLcode = undefined;
    curl = curl_easy_init();
    if (curl != null) {
        _ = curl_easy_setopt(curl, .CURLOPT_URL, "https://example.com");
        _ = curl_easy_setopt(curl, .CURLOPT_FOLLOWLOCATION, @as(c_long, 1));
        res = curl_easy_perform(curl);
        if (res != .CURLE_OK) _ = fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        curl_easy_cleanup(curl);
    }
    return 0;
}

But I think it's safe to say that this issue is solved.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.6.0 Feb 29, 2020
@lithdew
Copy link
Contributor

lithdew commented Jul 17, 2020

Just wanted to comment here for anyone trying to use libcurl that enum handling already works in the way @andrewrk mentioned.

const std = @import("std");
const c = @cImport(@cInclude("curl/curl.h"));

fn wrap(result: anytype) !void {
    switch (@enumToInt(result)) {
        c.CURLE_OK => return,
        else => unreachable,
    }
}

pub fn main() !void {
    const curl = c.curl_easy_init();
    if (curl == null) return error.InitFailed;
    defer c.curl_easy_cleanup(curl);

    try wrap(c.curl_easy_setopt(curl, .CURLOPT_URL, "https://client.tlsfingerprint.io:8443/"));
    try wrap(c.curl_easy_setopt(curl, .CURLOPT_FOLLOWLOCATION, @as(c_long, 1)));

    if (c.curl_easy_perform(curl) == .CURLE_OK) {
        std.debug.print("It works!\n", .{});
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants