Skip to content

Commit cd980b3

Browse files
committed
mk: Add support for musl-based builds
This commit adds support to the makefiles, configuration script, and build system to understand MUSL. This is broken up into a few parts: * Any target of the form `*-musl` requires the `--musl-root` option to `./configure` which will indicate the root of the MUSL installation. It is also expected that there is a libunwind build inside of that installation built against that MUSL. * Objects from MUSL are copied into the build tree for Rust to be statically linked into the appropriate Rust library. * Objects for binary startup and shutdown are included in each Rust installation by default for MUSL. This requires MUSL to only be installed on the machine compiling rust. Only a linker will be necessary for compiling against MUSL on a target machine. Eventually a MUSL and/or libunwind build may be integrated by default into the build but for now they are just always assumed to exist externally.
1 parent ba2380d commit cd980b3

File tree

7 files changed

+68
-14
lines changed

7 files changed

+68
-14
lines changed

configure

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ valopt jemalloc-root "" "set directory where libjemalloc_pic.a is located"
583583
valopt build "${DEFAULT_BUILD}" "GNUs ./configure syntax LLVM build triple"
584584
valopt android-cross-path "/opt/ndk_standalone" "Android NDK standalone path"
585585
valopt release-channel "dev" "the name of the release channel to build"
586+
valopt musl-root "/usr/local" "MUSL root installation directory"
586587

587588
# Many of these are saved below during the "writing configuration" step
588589
# (others are conditionally saved).
@@ -1058,6 +1059,13 @@ do
10581059
fi
10591060
;;
10601061

1062+
1063+
*-musl)
1064+
if [ ! -f $CFG_MUSL_ROOT/lib/libc.a ]
1065+
then
1066+
err "musl libc $CFG_MUSL_ROOT/lib/libc.a not found"
1067+
fi
1068+
;;
10611069
*)
10621070
;;
10631071
esac

mk/cfg/x86_64-unknown-linux-musl.mk

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# x86_64-unknown-linux-musl configuration
2+
CC_x86_64-unknown-linux-musl=$(CFG_MUSL_ROOT)/bin/musl-gcc
3+
CXX_x86_64-unknown-linux-musl=notaprogram
4+
CPP_x86_64-unknown-linux-musl=$(CFG_MUSL_ROOT)/bin/musl-gcc -E
5+
AR_x86_64-unknown-linux-musl=$(AR)
6+
CFG_LIB_NAME_x86_64-unknown-linux-musl=lib$(1).so
7+
CFG_STATIC_LIB_NAME_x86_64-unknown-linux-musl=lib$(1).a
8+
CFG_LIB_GLOB_x86_64-unknown-linux-musl=lib$(1)-*.so
9+
CFG_JEMALLOC_CFLAGS_x86_64-unknown-linux-musl := -m64
10+
CFG_GCCISH_CFLAGS_x86_64-unknown-linux-musl := -Wall -Werror -g -fPIC -m64
11+
CFG_GCCISH_CXXFLAGS_x86_64-unknown-linux-musl :=
12+
CFG_GCCISH_LINK_FLAGS_x86_64-unknown-linux-musl :=
13+
CFG_GCCISH_DEF_FLAG_x86_64-unknown-linux-musl :=
14+
CFG_LLC_FLAGS_x86_64-unknown-linux-musl :=
15+
CFG_INSTALL_NAME_x86_64-unknown-linux-musl =
16+
CFG_EXE_SUFFIX_x86_64-unknown-linux-musl =
17+
CFG_WINDOWSY_x86_64-unknown-linux-musl :=
18+
CFG_UNIXY_x86_64-unknown-linux-musl := 1
19+
CFG_LDPATH_x86_64-unknown-linux-musl :=
20+
CFG_RUN_x86_64-unknown-linux-musl=$(2)
21+
CFG_RUN_TARG_x86_64-unknown-linux-musl=$(call CFG_RUN_x86_64-unknown-linux-musl,,$(2))
22+
CFG_GNU_TRIPLE_x86_64-unknown-linux-musl := x86_64-unknown-linux-musl
23+
24+
NATIVE_DEPS_libc_T_x86_64-unknown-linux-musl += libc.a
25+
NATIVE_DEPS_std_T_x86_64-unknown-linux-musl += libunwind.a \
26+
crt1.o crti.o crtn.o
27+
INSTALLED_OBJECTS_x86_64-unknown-linux-musl += crt1.o crti.o crtn.o

mk/crates.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ ONLY_RLIB_rustc_bitflags := 1
123123
# Documented-by-default crates
124124
DOC_CRATES := std alloc collections core libc rustc_unicode
125125

126+
# Installed objects/libraries by default
127+
INSTALLED_OBJECTS := libmorestack.a libcompiler-rt.a
128+
126129
################################################################################
127130
# You should not need to edit below this line
128131
################################################################################

mk/main.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ endif
399399
# Prerequisites for using the stageN compiler to build target artifacts
400400
TSREQ$(1)_T_$(2)_H_$(3) = \
401401
$$(HSREQ$(1)_H_$(3)) \
402-
$$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a \
403-
$$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a
402+
$$(foreach obj,$$(INSTALLED_OBJECTS),\
403+
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(obj)) \
404+
$$(foreach obj,$$(INSTALLED_OBJECTS_$(2)),\
405+
$$(TLIB$(1)_T_$(2)_H_$(3))/$$(obj))
404406

405407
# Prerequisites for a working stageN compiler and libraries, for a specific
406408
# target

mk/prepare.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ prepare-target-$(2)-host-$(3)-$(1)-$(4): prepare-maybe-clean-$(4) \
140140
$$(if $$(findstring $(2),$$(CFG_HOST)), \
141141
$$(foreach crate,$$(HOST_CRATES), \
142142
$$(call PREPARE_LIB,$$(call CFG_LIB_GLOB_$(2),$$(crate)))),) \
143-
$$(call PREPARE_LIB,libmorestack.a) \
144-
$$(call PREPARE_LIB,libcompiler-rt.a),),),)
143+
$$(foreach object,$$(INSTALLED_OBJECTS) $$(INSTALLED_OBJECTS_$(2)),\
144+
$$(call PREPARE_LIB,$$(object))),),),)
145145
endef
146146

147147
define INSTALL_GDB_DEBUGGER_SCRIPTS_COMMANDS

mk/rt.mk

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ $$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.ll $$(MKFILE_DEPS) \
7474
@mkdir -p $$(@D)
7575
@$$(call E, compile: $$@)
7676
$$(Q)$$(LLC_$$(CFG_BUILD)) $$(CFG_LLC_FLAGS_$(1)) \
77-
-filetype=obj -mtriple=$$(CFG_LLVM_TARGET_$(1)) -relocation-model=pic -o $$@ $$<
77+
-filetype=obj -mtriple=$$(CFG_LLVM_TARGET_$(1)) \
78+
-relocation-model=pic -o $$@ $$<
7879

7980
$$(RT_OUTPUT_DIR_$(1))/%.o: $(S)src/rt/%.c $$(MKFILE_DEPS)
8081
@mkdir -p $$(@D)
@@ -110,6 +111,11 @@ $$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1)): $$(OBJS_$(2)_$(1))
110111
@$$(call E, link: $$@)
111112
$$(Q)$$(AR_$(1)) rcs $$@ $$^
112113

114+
ifeq ($$(findstring windows,$(1)),windows)
115+
$$(RT_OUTPUT_DIR_$(1))/lib$(2).a: $$(RT_OUTPUT_DIR_$(1))/$$(NATIVE_$(2)_$(1))
116+
$$(Q)cp $$< $$^
117+
endif
118+
113119
endef
114120

115121
$(foreach target,$(CFG_TARGET), \
@@ -312,6 +318,19 @@ endif # endif for windowsy
312318
endif # endif for ios
313319
endif # endif for darwin
314320

321+
################################################################################
322+
# libc/libunwind for musl
323+
#
324+
# When we're building a musl-like target we're going to link libc/libunwind
325+
# statically into the standard library and liblibc, so we need to make sure
326+
# they're in a location that we can find
327+
################################################################################
328+
329+
ifeq ($$(findstring musl,$(1)),musl)
330+
$$(RT_OUTPUT_DIR_$(1))/%: $$(CFG_MUSL_ROOT)/lib/%
331+
cp $$^ $$@
332+
endif
333+
315334
endef
316335

317336
# Instantiate template for all stages/targets

mk/target.mk

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ CRATE_FULLDEPS_$(1)_T_$(2)_H_$(3)_$(4) := \
3535
$$(foreach dep,$$(RUST_DEPS_$(4)), \
3636
$$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$$(dep)) \
3737
$$(foreach dep,$$(NATIVE_DEPS_$(4)), \
38-
$$(RT_OUTPUT_DIR_$(2))/$$(call CFG_STATIC_LIB_NAME_$(2),$$(dep)))
38+
$$(RT_OUTPUT_DIR_$(2))/$$(call CFG_STATIC_LIB_NAME_$(2),$$(dep))) \
39+
$$(foreach dep,$$(NATIVE_DEPS_$(4)_T_$(2)), \
40+
$$(RT_OUTPUT_DIR_$(2))/$$(dep))
3941
endef
4042

4143
$(foreach host,$(CFG_HOST), \
@@ -143,14 +145,7 @@ $$(TBIN$(1)_T_$(2)_H_$(3))/:
143145
$$(TLIB$(1)_T_$(2)_H_$(3))/:
144146
mkdir -p $$@
145147

146-
$$(TLIB$(1)_T_$(2)_H_$(3))/libcompiler-rt.a: \
147-
$$(RT_OUTPUT_DIR_$(2))/$$(call CFG_STATIC_LIB_NAME_$(2),compiler-rt) \
148-
| $$(TLIB$(1)_T_$(2)_H_$(3))/ $$(SNAPSHOT_RUSTC_POST_CLEANUP)
149-
@$$(call E, cp: $$@)
150-
$$(Q)cp $$< $$@
151-
152-
$$(TLIB$(1)_T_$(2)_H_$(3))/libmorestack.a: \
153-
$$(RT_OUTPUT_DIR_$(2))/$$(call CFG_STATIC_LIB_NAME_$(2),morestack) \
148+
$$(TLIB$(1)_T_$(2)_H_$(3))/%: $$(RT_OUTPUT_DIR_$(2))/% \
154149
| $$(TLIB$(1)_T_$(2)_H_$(3))/ $$(SNAPSHOT_RUSTC_POST_CLEANUP)
155150
@$$(call E, cp: $$@)
156151
$$(Q)cp $$< $$@

0 commit comments

Comments
 (0)