From 8a91c52fa24920470e4d8ccd161c8b83be3e5f09 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Wed, 27 Nov 2019 14:52:20 -0600 Subject: [PATCH] Change pthreads linking with jpm, make, and meson. Try to add pthread deps when compiling programs with jpm. --- Makefile | 4 ++-- auxbin/jpm | 14 +++++++++++--- meson.build | 11 ++++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 1c00e317..ed1eb0ce 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ INCLUDEDIR?=$(PREFIX)/include BINDIR?=$(PREFIX)/bin LIBDIR?=$(PREFIX)/lib JANET_BUILD?="\"$(shell git log --pretty=format:'%h' -n 1)\"" -CLIBS=-lm +CLIBS=-lm -lpthread JANET_TARGET=build/janet JANET_LIBRARY=build/libjanet.so JANET_STATIC_LIBRARY=build/libjanet.a @@ -37,7 +37,7 @@ MANPATH?=$(PREFIX)/share/man/man1/ PKG_CONFIG_PATH?=$(LIBDIR)/pkgconfig DEBUGGER=gdb -CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -Isrc/conf -fPIC -O2 -fvisibility=hidden -pthread \ +CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -Isrc/conf -fPIC -O2 -fvisibility=hidden \ -DJANET_BUILD=$(JANET_BUILD) LDFLAGS=-rdynamic diff --git a/auxbin/jpm b/auxbin/jpm index 758db9b2..9e0ca2d2 100755 --- a/auxbin/jpm +++ b/auxbin/jpm @@ -112,6 +112,10 @@ (def default-linker (if is-win "link" "cc")) (def default-archiver (if is-win "lib" "ar")) +# Detect threads +(def env (fiber/getenv (fiber/current))) +(def threads? (not (not (env 'threads/from-image)))) + # Default flags for natives, but not required (def default-lflags (if is-win ["/nologo"] [])) (def default-cflags @@ -119,6 +123,10 @@ ["/nologo" "/MD"] ["-std=c99" "-Wall" "-Wextra"])) + +# Link to pthreads +(def- thread-flags (if is-win [] (if threads? ["-lpthread"] []))) + # Required flags for dynamic libraries. These # are used no matter what for dynamic libraries. (def- dynamic-cflags @@ -127,10 +135,10 @@ ["-fPIC"])) (def- dynamic-lflags (if is-win - ["/DLL"] + ["/DLL" ;thread-flags] (if is-mac - ["-shared" "-undefined" "dynamic_lookup"] - ["-shared"]))) + ["-shared" "-undefined" "dynamic_lookup" ;thread-flags] + ["-shared" ;thread-flags]))) (defn- opt "Get an option, allowing overrides via dynamic bindings AND some diff --git a/meson.build b/meson.build index 7bf00e87..c072e0b4 100644 --- a/meson.build +++ b/meson.build @@ -30,6 +30,7 @@ header_path = join_paths(get_option('prefix'), get_option('includedir'), 'janet' cc = meson.get_compiler('c') m_dep = cc.find_library('m', required : false) dl_dep = cc.find_library('dl', required : false) +thread_dep = dependency('threads') # Link options if build_machine.system() != 'windows' @@ -156,7 +157,7 @@ mainclient_src = [ janet_boot = executable('janet-boot', core_src, boot_src, boot_gen, include_directories : incdir, c_args : '-DJANET_BOOTSTRAP', - dependencies : [m_dep, dl_dep], + dependencies : [m_dep, dl_dep, thread_dep], native : true) # Build core image @@ -167,7 +168,7 @@ core_image = custom_target('core_image', libjanet = library('janet', core_src, core_image, include_directories : incdir, - dependencies : [m_dep, dl_dep], + dependencies : [m_dep, dl_dep, thread_dep], install : true) # Extra c flags - adding -fvisibility=hidden matches the Makefile and @@ -187,14 +188,14 @@ endif janet_mainclient = executable('janet', core_src, core_image, mainclient_src, include_directories : incdir, - dependencies : [m_dep, dl_dep], + dependencies : [m_dep, dl_dep, thread_dep], c_args : extra_native_cflags, install : true) if meson.is_cross_build() janet_nativeclient = executable('janet-native', core_src, core_image, mainclient_src, include_directories : incdir, - dependencies : [m_dep, dl_dep], + dependencies : [m_dep, dl_dep, thread_dep], c_args : extra_cross_cflags, native : true) else @@ -224,7 +225,7 @@ amalg_shell = custom_target('amalg-shell', # Amalgamated client janet_amalgclient = executable('janet-amalg', amalg, amalg_shell, include_directories : incdir, - dependencies : [m_dep, dl_dep], + dependencies : [m_dep, dl_dep, thread_dep], build_by_default : false) # Tests