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

Bake should use libtool instead of ar on OSX #176

Closed
flxo opened this issue Jul 1, 2019 · 4 comments
Closed

Bake should use libtool instead of ar on OSX #176

flxo opened this issue Jul 1, 2019 · 4 comments
Assignees
Labels

Comments

@flxo
Copy link
Member

flxo commented Jul 1, 2019

Building a stupid simple binary that depends on a static lib fails on OSX because ar can't be used with the latest Xcode clang:

~/bake  cat main/Project.meta

Project default: Main {
  ExecutableConfig Main {
    Files "main.cpp"

    Dependency lib
    DefaultToolchain CLANG
  }
}

~/bake  cat lib/Project.meta

Project default: Lib {
  LibraryConfig Lib {
    Files "lib.cpp"
    IncludeDir ".", inherit: true
    DefaultToolchain CLANG
  }
}

~/bake  cat main/main.cpp
#include "lib.h"

int main() {
    test();
}

~/bake  cat lib/lib.*
void test() {
}
void test();
~/bake/main  bake --rebuild
**** Building 1 of 2: lib (Lib) ****
**** Building 2 of 2: main (Main) ****
Compiling lib (Lib): lib.cpp
Compiling main (Main): main.cpp
Creating  lib (Lib): build/Lib_main_Main/liblib.a
ar: creating build/Lib_main_Main/liblib.a
Linking   main (Main): build/Main/main
clang++ -o build/Main/main build/Main/main.o ../lib/build/Lib_main_Main/liblib.a
ld: warning: ignoring file ../lib/build/Lib_main_Main/liblib.a, file was built for archive which is not the architecture being linked (x86_64): ../lib/build/Lib_main_Main/liblib.a
Undefined symbols for architecture x86_64:
  "test()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Rebuilding failed.

Patching the clang toolchain definition helps:

CLANG_CHAIN[:ARCHIVER][:COMMAND] = "libtool"
CLANG_CHAIN[:ARCHIVER][:ARCHIVE_FLAGS] = "-static -o"

but should be done in a proper host specific way.

@flxo flxo added the bug label Jul 1, 2019
@flxo
Copy link
Member Author

flxo commented Jul 17, 2019

I investigated in this issue and tried to naive approach:

In `lib/bake/toolchain/clang.rb:

    if Bake::Utils::OS::name == "Mac"
      CLANG_CHAIN[:ARCHIVER][:COMMAND] = "libtool"
      CLANG_CHAIN[:ARCHIVER][:ARCHIVE_FLAGS] = "-static -o"
    else
      CLANG_CHAIN[:ARCHIVER][:COMMAND] = "ar"
      CLANG_CHAIN[:ARCHIVER][:ARCHIVE_FLAGS] = "r"
    end

This works for non cross builds.

For the cross compilation use case it's quite common to overwrite the commands for the compilers and the archiver. The archiver could be ar and bake keeps applying the flags for the host - on a mac: -static -o. This results in e.g arm-linux-androideabi-ar -static -o.
Bake currently provides no way to replace the flags from the toolchain file with custom ones. The Toolchain or DefaultToolchain section a Project.meta just allows to add flags to the predefined ones.
This is quite confusing since the syntax is:

Toolchain {
  Archiver {
    Flags add: "-foo"
    or
    Flags "-bar"
  }

In fact the Flags attribute doesn't affect the hardcoded ones from the toolchain config baked into bake which I consider as a restriction -> no possibility to have a fully custom configuration.

@aschaal
Copy link
Contributor

aschaal commented Jul 25, 2019

Yes, the Flag definition is confusing, but it's hard to change because of breaking too many applications. We need to think about how it should be in future and how we come to this, e.g. by making it deprecated, printing an info log etc...

Changing the built-in flags is not possible by intention. It's more than just flags, but e.g. reading out dependency files, parsing compiler messages, etc.
--> If you need something, just tell me, I will add/change compiler definitions for you. Like this one:

I will change the CLANG definition for you as suggested:

    if Bake::Utils::OS::name == "Mac"
      CLANG_CHAIN[:ARCHIVER][:COMMAND] = "libtool"
      CLANG_CHAIN[:ARCHIVER][:ARCHIVE_FLAGS] = "-static -o"
    else
      CLANG_CHAIN[:ARCHIVER][:COMMAND] = "ar"
      CLANG_CHAIN[:ARCHIVER][:ARCHIVE_FLAGS] = "r"
    end

@aschaal
Copy link
Contributor

aschaal commented Jul 25, 2019

Will be fixed in next release.

@aschaal
Copy link
Contributor

aschaal commented Jul 26, 2019

Fixed in 2.53.0

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

No branches or pull requests

2 participants