From 9074100199d9c82b6ee5ca40606c3e2057092350 Mon Sep 17 00:00:00 2001 From: cancel Date: Thu, 12 Nov 2020 10:48:10 +0900 Subject: [PATCH] Add use of pkg-config for Linux ncurses link args Depending on the Linux distro, ncurses may be built with tinfo as a separate library that needs to be explicitly linked, or it may not. Trying to pass -ltinfo when you don't need to might cause a linking error. Failing to pass -ltinfo when you need to might cause a linking error. And you might need to pass -ltinfow instead of -ltinfo, or you might not. And if you get that wrong, you might cause a linking error. This commit adds use pkg-config to the tool build script, attempting to discover what args to use. This is only done on Linux. On other platforms, or if pkg-config returns an error, we use the same hard-coded options as before: -lncursesw -lformw This probably adds about 5 or 10 milliseconds to the execution time of the tool script. --- tool | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tool b/tool index d5a93c0..cba07a8 100755 --- a/tool +++ b/tool @@ -409,7 +409,23 @@ build_target() { add cc_flags -D_POSIX_C_SOURCE=200809L ;; esac - add libraries -lformw -lncursesw + # Depending on the Linux distro, ncurses might have been built with tinfo + # as a separate library that explicitly needs to be linked, or it might + # not. And if it does, it might need to be either -ltinfo or -ltinfow. + # Yikes. If this is Linux, let's try asking pkg-config what it thinks. + local curses_flags=0 + if [[ $os == linux ]]; then + if curses_flags=$(pkg-config --libs ncursesw formw 2>/dev/null); then + # append flags to array, splitting on spaces + IFS=" " read -r -a libraries <<< "$curses_flags" + curses_flags=1 + fi + fi + # If we didn't get the flags by pkg-config, just guess. (This will work + # most of the time, including on Mac with Homebrew, and cygwin.) + if [[ $curses_flags = 0 ]]; then + add libraries -lncursesw -lformw + fi if [[ $portmidi_enabled = 1 ]]; then add libraries -lportmidi add cc_flags -DFEAT_PORTMIDI