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

nimsuggest: failed to build with clang on musl libc #20886

Closed
dm9pZCAq opened this issue Nov 21, 2022 · 3 comments · Fixed by #24313
Closed

nimsuggest: failed to build with clang on musl libc #20886

dm9pZCAq opened this issue Nov 21, 2022 · 3 comments · Fixed by #24313

Comments

@dm9pZCAq
Copy link

What happened?

failed to build nimsuggest with clang on musl libc because of incorrect type of Thread.sys

in nim SysThread defined here:
https://github.com/nim-lang/Nim/blob/v1.6.8/lib/system/threadlocalstorage.nim#L131
in devel branch it is defined as culong:

SysThread* {.importc: "pthread_t",
header: "<sys/types.h>" .} = distinct culong

but in /usr/include/bits/alltypes.h it is defined as struct pointer:

#ifdef __cplusplus
#	if defined(__NEED_pthread_t) && !defined(__DEFINED_pthread_t)
typedef unsigned long pthread_t;
#		define __DEFINED_pthread_t
#	endif

#else

#	if defined(__NEED_pthread_t) && !defined(__DEFINED_pthread_t)
typedef struct __pthread* pthread_t;
#		define __DEFINED_pthread_t
#	endif

#endif

Nim Version

trying to build: nim-1.6.8

Current Standard Output Logs

CC: nimsuggest.nim
/var/tmp/portage/dev-lang/nim-1.6.8/homedir/.cache/nim/nimsuggest_r/@mnimsuggest.nim.c:4818:21: error:
      incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'pthread_t'
      (aka 'struct __pthread *') [-Wint-conversion]
        T1_ = pthread_join((*t).sys, ((void**) NIM_NIL));
                           ^~~~~~~~
/usr/include/pthread.h:83:27: note: passing argument to parameter here
int pthread_join(pthread_t, void **);
                          ^
1 error generated.
Error: execution of an external compiler program 'x86_64-gentoo-linux-musl-clang -c -w -fmax-errors=3 -pthread -march=native -O3 -flto -fPIC -falign-functions=32 -funroll-loops -fno-math-errno -fno-trapping-math -fno-math-errno -ffinite-m>


FAILURE

Expected Standard Output Logs

No response

Possible Solution

No response

Additional Information

$ clang --version
clang version: 15.0.2
Target: x86_64-gentoo-linux-musl


musl version: 1.2.3


@dm9pZCAq
Copy link
Author

here is fix for nim 1.6.12:

diff --git a/lib/system/threadlocalstorage.nim b/lib/system/threadlocalstorage.nim
--- a/lib/system/threadlocalstorage.nim
+++ b/lib/system/threadlocalstorage.nim
@@ -129,7 +129,7 @@ else:
   when (defined(linux) or defined(nintendoswitch)) and defined(amd64):
     type
       SysThread* {.importc: "pthread_t",
-                  header: "<sys/types.h>" .} = distinct culong
+                  header: "<sys/types.h>" .} = distinct pointer
       Pthread_attr {.importc: "pthread_attr_t",
                     header: "<sys/types.h>".} = object
         abi: array[56 div sizeof(clong), clong]

with this change everything compiles fine

i think this should be done somehow with when keyword, but i didn't found out how to check if nim compiles on musl with when


when replacing with object instead of pointer:

/var/tmp/portage/dev-lang/nim-1.6.12/homedir/.cache/nim/nimsuggest_r/@m..@slib@ssystem.nim.c:76:26: error:
      typedef redefinition with different types ('struct pthread_t' vs 'struct __pthread *')
typedef struct pthread_t pthread_t;
                         ^
/usr/include/bits/alltypes.h:273:28: note: previous definition is here
typedef struct __pthread * pthread_t;
                           ^

@listout
Copy link

listout commented Jul 31, 2023

hello, facing the same issue. Any updates on this one?

@Araq Araq closed this as completed in 8be82c3 Oct 16, 2024
narimiran pushed a commit that referenced this issue Jan 14, 2025
…tinct (#24313)

fixes #18896
fixes #20886

```nim
type
  PFile {.importc: "FILE*", header: "<stdio.h>".} = distinct pointer
    # import C's FILE* type; Nim will treat it as a new pointer type
```
This is an excerpt from the Nim manual. In the old Nim versions, it
produces a void pointer type instead of the `FILE*` type that should
have been generated. Because these C types tend to be opaque and adapt
to changes on different platforms. It might affect the portability of
Nim on various OS, i.e. `csource_v2` cannot build on the apline platform
because of `Time` relies on Nim types instead of the `time_t` type.

ref #18851

(cherry picked from commit 8be82c3)
@arkanoid87
Copy link
Contributor

arkanoid87 commented Jan 30, 2025

facing this issue on Nim Compiler Version 2.2.0 [Linux: amd64]

Proposed change still works but now is in lib/std/private/threadtypes.nim

SysThread* {.importc: "pthread_t", header: "<sys/types.h>" .} = distinct pointer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants