Skip to content

Improve the linking of libraries #1338

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

Merged
merged 1 commit into from
Sep 15, 2016

Conversation

akosthekiss
Copy link
Member

Although both jerry-libc and jerry-libm have configuration options
that enable/disable their build, in practice, only jerry-libc can be
replaced with the system (compiler-default) libc. If jerry-libm is
disabled, the build of jerry-main fails, as there is no way to
instruct the linker to link the system libm to the binary. (The
build system does have a way to pass flags to the linker, but those
flags are listed before the linked objects. For the references to
get resolved correctly, the libraries to be linked have to be
specified after the objects.)

This patch adds the EXTERNAL_LINK_LIBS configuration option to
CMakeLists, which ensures that the specified libraries get
correctly passed to the linker. (E.g, replacing jerry-libm with
system libm becomes possible with
JERRY_LIBM=OFF EXTERNAL_LINK_LIBS='-lm'.)

Additionally, the patch also makes the following related changes:

  • Removes the COMPILER_DEFAULT_LIBC configuration option, as it is
    (almost) always the opposite of JERRY_LIBC. Moreover, its name is
    misleading: its only role is to add -nostdlib to the linker
    flags.
  • Makes use of transitive library dependencies: if a library has
    another library as dependency, and it is linked to a binary, its
    dependency is linked as well. Thus, jerry-libc, jerry-libm, and
    any external libraries are added to jerry-core as dependency, and
    then only jerry-core is linked to executables (cmake will take
    care of the rest).
  • build.py and run-tests.py follow up the changes, along with some
    minor syntax changes.
  • Moves static linking option to global CMakeLists, as unit test
    binaries should be linked the same way as jerry-main.
  • Adds EXTERNAL_COMPILER_FLAGS and EXTERNAL_LINKER_FLAGS as last to
    the flag list, to allow user override of (nearly) anything.

The patch speculatively follows up the build system changes in the
mbed, riot-stm32f4, and zephyr targets.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]

@akosthekiss akosthekiss added enhancement An improvement tools Related to the tooling scripts labels Sep 11, 2016
@akosthekiss
Copy link
Member Author

Example use cases:

Default, e.g., statically linked Linux binaries:
JERRY_LIBC=ON JERRY_LIBM=ON ENABLE_STATIC_LINK=ON

Linux or Tizen binaries dynamically linked to system libs:
JERRY_LIBC=OFF JERRY_LIBM=OFF EXTERNAL_LINK_LIBS='-lm' ENABLE_STATIC_LINK=OFF

OS X binaries:
JERRY_LIBC=OFF JERRY_LIBM=OFF ENABLE_STATIC_LINK=OFF
('-lSystem' is automatically added by the build system)

Experiment results on x86-64/Linux:

  • tools/build.py: (default opts)
    • ldd: not a dynamic executable
    • size: 233573 text, 16 data, 2626072 bss
  • tools/build.py: --jerry-libc=off
    • ldd: not a dynamic executable
    • size: 1012949 text, 7764 data, 2635776 bss
  • tools/build.py: --jerry-libc=off --jerry-libm=off --link-lib='-lm'
    • ldd: not a dynamic executable
    • size: 1843941 text, 7900 data, 2635776 bss
  • tools/build.py: --static-link=off
    • ldd: not a dynamic executable
    • size: 236517 text, 16 data, 2626072 bss
  • tools/build.py: --jerry-libc=off --static-link=off
    • ldd: linux-vdso.so.1, libc.so.6, /lib64/ld-linux-x86-64.so.2
    • size: 232376 text, 704 data, 2626136 bss
  • tools/build.py: --jerry-libc=off --jerry-libm=off --link-lib='-lm' --static-link=off
    • ldd: linux-vdso.so.1, libc.so.6, libm.so.6, /lib64/ld-linux-x86-64.so.2
    • size: 219850 text, 848 data, 2626136 bss

@robertsipka
Copy link
Contributor

I like this patch, especially that you removed the the COMPILER_DEFAULT_LIBC option, it was quite confusing. Could you please also update the GETTING-STARTED documentation?

@@ -201,13 +201,19 @@ add_library(${JERRY_CORE_NAME} STATIC ${SOURCE_CORE})
target_compile_definitions(${JERRY_CORE_NAME} PUBLIC ${DEFINES_JERRY})
target_include_directories(${JERRY_CORE_NAME} PUBLIC ${INCLUDE_CORE})

if (JERRY_LIBC)
set(JERRY_LIBS )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the space before the left parenthesis intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, JERRY_LIBS is set to empty string. Without that space it just looked like a predicate ("if(JERRY_LIBS)") to me.

@LaszloLango
Copy link
Contributor

LGTM

@akosthekiss
Copy link
Member Author

@sergioamr Could you please take a look at the zephyr-related part? I did my best guess not to break the build there but could not try it in practice.

@sergioamr
Copy link
Contributor

@poussa Sakari, i will be in the thingmonk conference this week so i will probably don't have access to build this. Could you have a look? Thanks

Although both jerry-libc and jerry-libm have configuration options
that enable/disable their build, in practice, only jerry-libc can be
replaced with the system (compiler-default) libc. If jerry-libm is
disabled, the build of jerry-main fails, as there is no way to
instruct the linker to link the system libm to the binary. (The
build system does have a way to pass flags to the linker, but those
flags are listed before the linked objects. For the references to
get resolved correctly, the libraries to be linked have to be
specified _after_ the objects.)

This patch adds the EXTERNAL_LINK_LIBS configuration option to
CMakeLists, which ensures that the specified libraries get
correctly passed to the linker. (E.g, replacing jerry-libm with
system libm becomes possible with
`JERRY_LIBM=OFF EXTERNAL_LINK_LIBS='-lm'`.)

Additionally, the patch also makes the following related changes:

* Removes the COMPILER_DEFAULT_LIBC configuration option, as it is
  (almost) always the opposite of JERRY_LIBC. Moreover, its name is
  misleading: its only role is to add `-nostdlib` to the linker
  flags.

* Makes use of transitive library dependencies: if a library has
  another library as dependency, and it is linked to a binary, its
  dependency is linked as well. Thus, jerry-libc, jerry-libm, and
  any external libraries are added to jerry-core as dependency, and
  then only jerry-core is linked to executables (cmake will take
  care of the rest).

* build.py and run-tests.py follow up the changes, along with some
  minor syntax changes.

* Moves static linking option to global CMakeLists, as unit test
  binaries should be linked the same way as jerry-main.

* Adds EXTERNAL_COMPILER_FLAGS and EXTERNAL_LINKER_FLAGS as last to
  the flag list, to allow user override of (nearly) anything.

The patch speculatively follows up the build system changes in the
mbed, riot-stm32f4, and zephyr targets.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
@akosthekiss
Copy link
Member Author

@robertsipka Thanks for reminding me of the docs. Updated.

@dbatyai
Copy link
Member

dbatyai commented Sep 14, 2016

LGTM

@akosthekiss
Copy link
Member Author

@sergioamr @poussa Any feedback that would block landing this?

@sergioamr
Copy link
Contributor

@akosthekiss Sorry, still at Thinkmonk conference. Tomorrow i will be back in the office.

@poussa
Copy link
Contributor

poussa commented Sep 14, 2016

Sorry here as well - did not had time for this recently.

@sergioamr
Copy link
Contributor

LGTM, sorry about the delay

@akosthekiss akosthekiss merged commit d38ab71 into jerryscript-project:master Sep 15, 2016
@akosthekiss akosthekiss deleted the link-libs branch September 15, 2016 11:07
@akosthekiss
Copy link
Member Author

@sergioamr Thanks a lot!

bsdelf pushed a commit to bsdelf/jerryscript that referenced this pull request Sep 18, 2016
Although both jerry-libc and jerry-libm have configuration options
that enable/disable their build, in practice, only jerry-libc can be
replaced with the system (compiler-default) libc. If jerry-libm is
disabled, the build of jerry-main fails, as there is no way to
instruct the linker to link the system libm to the binary. (The
build system does have a way to pass flags to the linker, but those
flags are listed before the linked objects. For the references to
get resolved correctly, the libraries to be linked have to be
specified _after_ the objects.)

This patch adds the EXTERNAL_LINK_LIBS configuration option to
CMakeLists, which ensures that the specified libraries get
correctly passed to the linker. (E.g, replacing jerry-libm with
system libm becomes possible with
`JERRY_LIBM=OFF EXTERNAL_LINK_LIBS='-lm'`.)

Additionally, the patch also makes the following related changes:

* Removes the COMPILER_DEFAULT_LIBC configuration option, as it is
  (almost) always the opposite of JERRY_LIBC. Moreover, its name is
  misleading: its only role is to add `-nostdlib` to the linker
  flags.

* Makes use of transitive library dependencies: if a library has
  another library as dependency, and it is linked to a binary, its
  dependency is linked as well. Thus, jerry-libc, jerry-libm, and
  any external libraries are added to jerry-core as dependency, and
  then only jerry-core is linked to executables (cmake will take
  care of the rest).

* build.py and run-tests.py follow up the changes, along with some
  minor syntax changes.

* Moves static linking option to global CMakeLists, as unit test
  binaries should be linked the same way as jerry-main.

* Adds EXTERNAL_COMPILER_FLAGS and EXTERNAL_LINKER_FLAGS as last to
  the flag list, to allow user override of (nearly) anything.

The patch speculatively follows up the build system changes in the
mbed, riot-stm32f4, and zephyr targets.

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement tools Related to the tooling scripts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants