c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Fails to build on NetBSD.

Open SirWumpus opened this issue 5 months ago • 16 comments

  1. NetBSD supports POSIX API and so this change skips the pragma warning.
elf$ git diff
diff --git a/dependencies/miniz/miniz.c b/dependencies/miniz/miniz.c
index e6501010..a60ca1af 100644
--- a/dependencies/miniz/miniz.c
+++ b/dependencies/miniz/miniz.c
@@ -3165,7 +3165,7 @@ static int mz_stat64(const char *path, struct __stat64 *buffer)
 #define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
 #define MZ_DELETE_FILE remove

-#elif defined(__APPLE__) || defined(__FreeBSD__)
+#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
 #ifndef MINIZ_NO_TIME
 #include <utime.h>
 #endif
elf$
  1. With the above change this error occurs. I have not looked into at this time.
[ 71%] Building C object CMakeFiles/c3c.dir/src/utils/whereami.c.o
/usr/local/src/c3c/src/utils/whereami.c: In function 'get_executable_path_raw':
/usr/local/src/c3c/src/utils/whereami.c:404:17: error: 'free' called on unallocated object 'buffer1' [-Werror=free-nonheap-object]
  404 |                 free(path);
      |                 ^~~~~~~~~~
/usr/local/src/c3c/src/utils/whereami.c:353:14: note: declared here
  353 |         char buffer1[PATH_MAX];
      |              ^~~~~~~
cc1: all warnings being treated as errors

SirWumpus avatar Jul 10 '25 00:07 SirWumpus

In get_executable_path_raw() both buffer1 and buffer2 are local variables, so impossible to free via path. See patch.

diff --git a/dependencies/miniz/miniz.c b/dependencies/miniz/miniz.c
index e6501010..a60ca1af 100644
--- a/dependencies/miniz/miniz.c
+++ b/dependencies/miniz/miniz.c
@@ -3165,7 +3165,7 @@ static int mz_stat64(const char *path, struct __stat64 *buffer)
 #define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
 #define MZ_DELETE_FILE remove

-#elif defined(__APPLE__) || defined(__FreeBSD__)
+#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
 #ifndef MINIZ_NO_TIME
 #include <utime.h>
 #endif
diff --git a/src/utils/whereami.c b/src/utils/whereami.c
index ada53124..bc3c6d2b 100644
--- a/src/utils/whereami.c
+++ b/src/utils/whereami.c
@@ -399,11 +399,6 @@ static int get_executable_path_raw(char *out, int capacity, int *dirname_length)
                break;
        }

-       if (path != buffer1)
-       {
-               free(path);
-       }
-
        return length;
 }

SirWumpus avatar Jul 10 '25 15:07 SirWumpus

I've updated whereami.c now, does it work better?

lerno avatar Jul 16 '25 14:07 lerno

What is the state of this now?

lerno avatar Jul 30 '25 09:07 lerno

After pulling and rebuilding on NetBSD, part 1 of the OP...

diff --git a/dependencies/miniz/miniz.c b/dependencies/miniz/miniz.c
index e6501010..a60ca1af 100644
--- a/dependencies/miniz/miniz.c
+++ b/dependencies/miniz/miniz.c
@@ -3165,7 +3165,7 @@ static int mz_stat64(const char *path, struct __stat64 *buffer)
 #define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
 #define MZ_DELETE_FILE remove

-#elif defined(__APPLE__) || defined(__FreeBSD__)
+#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
 #ifndef MINIZ_NO_TIME
 #include <utime.h>
 #endif

Was missing from the updated pulled source.

Applying the above change, c3c builds clean. I could not find mention of running a test suite other than this ./c3c compile ../resources/examples/hash.c3 (which does not compile clean).

elf$ ./c3c compile ../resources/examples/hash.c3
 1: module std::io::os;
 2: import libc, std::os, std::io;
 3:
 4: fn void? native_stat(Stat* stat, String path) @if(env::DARWIN || env::LINUX || env::ANDROID || env::BSD_FAMILY) => |
                         ^^^^^
(/usr/local/src/c3c/lib/std/io/os/fileinfo.c3:4:22) Error: 'Stat' could not be found, did you spell it right?

 6:
 7: struct Posix_dirent
 8: {
 9:     Ino_t d_fileno;
        ^^^^^
(/usr/local/src/c3c/lib/std/os/posix/files.c3:9:2) Error: 'Ino_t' could not be found, did you spell it right?

elf$ 

SirWumpus avatar Jul 30 '25 14:07 SirWumpus

Is it still not working?

lerno avatar Aug 27 '25 10:08 lerno

The suggested test example:

./c3c compile-run ../resources/testfragments/helloworld.c3

Continues to generate errors.

SirWumpus avatar Aug 27 '25 11:08 SirWumpus

What's necessary is to create a netbsd.c3 file next to these: https://github.com/c3lang/c3c/blob/master/lib/std/libc/os/freebsd.c3 https://github.com/c3lang/c3c/blob/master/lib/std/libc/os/openbsd.c3

With the correct sizes. It ought to compile then. Are you able to check the layouts?

lerno avatar Sep 01 '25 19:09 lerno

What's necessary is to create a netbsd.c3 file next to these: https://github.com/c3lang/c3c/blob/master/lib/std/libc/os/freebsd.c3 https://github.com/c3lang/c3c/blob/master/lib/std/libc/os/openbsd.c3

With the correct sizes. It ought to compile then. Are you able to check the layouts?

Oh. I'll have a stickybeak.

SirWumpus avatar Sep 03 '25 15:09 SirWumpus

Any progress?

lerno avatar Sep 10 '25 21:09 lerno

I started on it, but was distracted before I could verify I had it correct. I'll build/test over the weekend and answer.

SirWumpus avatar Sep 12 '25 11:09 SirWumpus

Great!

lerno avatar Sep 12 '25 15:09 lerno

Ping

lerno avatar Oct 10 '25 16:10 lerno

Sorry. Resuming work on it today.

SirWumpus avatar Oct 14 '25 15:10 SirWumpus

So I've synced my repo tried to build only to find now that you need -Wno-char-subscripts; I've added to CMakeLists.txt, but I'm no CMake boffin (prefer classic make(1)). With that it builds.

Next I try this...

elf$ ./c3c compile ../resources/examples/hash.c3
 9: alias Blkcnt_t = long;
10: alias Blksize_t = int;
11: alias Fflags_t = uint;
12: alias Off_t = long;
          ^^^^^^^^^^^^
(/home/achowe/git/c3c/lib/std/libc/os/netbsd.c3:12:7) Error: 'Off_t' would shadow a previous declaration.

 80: const bool BSD_FLAVOR_SIG @local = env::DARWIN || env::BSD_FAMILY;
 81:
 82: alias Time_t = $typefrom(env::WIN32 ? long.typeid : CLong.typeid);
 83: alias Off_t = $typefrom(env::WIN32 ? int.typeid : usz.typeid);
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(/home/achowe/git/c3c/lib/std/libc/libc.c3:83:7) Note: The previous use of 'Off_t' was here.

Which I half expected to fail.

  • First Off_t should be a signed type if it matches off_t as used by lseek, fseeko, ftello since it possible to seek relative the current file position SEEK_CUR (see SUS). So making it an usz.typeid might be wrong idea.

  • I do not know c3c yet (thus reason I want to build it to try). Is there some way to unalias or override an alias (suppose I need learn $typefrom syntax)? I suppose I could create a (temporary) posix_off_t for the purpose of testing stuff.

So that's where I'm at for the moment.

SirWumpus avatar Oct 18 '25 19:10 SirWumpus

Hey folks. I've made some progress on this. I've got c3c building and seemingly working at least for basic stuff on NetBSD aarch64. I chose aarch64 because that's what I have, and also the NetBSD community spends a lot of time developing on arm machines.

I replicated the changes discussed above and added a lib/std/libc/os/netbsd.c3. I based the type sizes and the layout of the Stat struct on the NetBSD man pages along with what was actually in the <sys/stat.h> header on NetBSD, so I think it's basically right, but I haven't tested extensively yet. At the very least, it's a start, I think.

To get the compiler working on NetBSD aarch64 I also had to add some stuff so that the proper setjmp size gets set and so that llvm compiles and links properly.

In line with the discussion above, I also changed the default for Off_t to isz, since it should be signed on POSIX platforms. isz isn't quite correct, since I believe there are OSes where off_t is always 64 bits (even on 32-bit platforms), but I think it's a better default than usz.

I haven't added any tests yet as I was just trying to see if I could get it to build and run, and these changes are just kind of the minimum necessary to get something working, but here's what I've got so far https://github.com/c3lang/c3c/compare/master...limit-ordinal:c3c:netbsd-aarch64-compile

limit-ordinal avatar Dec 12 '25 00:12 limit-ordinal

With the compiler building and running on NetBSD, I'm now trying to get things to the point where tests are all passing and the main compiler features work.

EDIT: All unit tests are now passing, as is the compiler test suite! Getting pretty close to done with this. I still need to manually check some things and add CI.

The diff for your convenience: https://github.com/c3lang/c3c/compare/master...limit-ordinal:c3c:netbsd-aarch64-compile

limit-ordinal avatar Dec 15 '25 02:12 limit-ordinal