Skip to content

Commit

Permalink
macos: Explicitly set the macOS version in the triple to avoid linker…
Browse files Browse the repository at this point in the history
… warnings and support LTO (ldc-developers#4534)

Obey MACOSX_DEPLOYMENT_TARGET, and if it is not set, then default to host OS version.
  • Loading branch information
JohanEngelen authored Nov 28, 2023
1 parent fe74d37 commit 93b9bab
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions driver/targetmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "driver/targetmachine.h"

#include "dmd/errors.h"
#include "driver/args.h"
#include "driver/cl_options.h"
#include "gen/logger.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -440,9 +441,30 @@ createTargetMachine(const std::string targetTriple, const std::string arch,
triple = llvm::Triple(
llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple()));

// We only support OSX, so darwin should really be macosx.
if (triple.getOS() == llvm::Triple::Darwin) {
triple.setOS(llvm::Triple::MacOSX);
// We only support OSX, so darwin should really be macosx.
llvm::SmallString<16> osname;
osname += "macosx";
// We have to specify OS version in the triple to avoid linker warnings,
// see https://github.com/ldc-developers/ldc/issues/4501.
// If environment variable MACOSX_DEPLOYMENT_TARGET is not set, then use
// host OS version.
// https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/cross_development/Configuring/configuring.html
std::string envVersion = env::get("MACOSX_DEPLOYMENT_TARGET");
if (!envVersion.empty()) {
osname += envVersion;
} else {
#if LDC_LLVM_VER >= 1400
llvm::VersionTuple OSVersion;
triple.getMacOSXVersion(OSVersion);
osname += OSVersion.getAsString();
#else
// Hardcode the version, because `getMacOSXVersion` is not available.
osname += "10.7";
#endif
}

triple.setOSName(osname);
}

// Handle -m32/-m64.
Expand Down

0 comments on commit 93b9bab

Please sign in to comment.