lib-std.m4 revision 2024:cc16e2a24f94
1#
2# Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# This code is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 2 only, as
7# published by the Free Software Foundation.  Oracle designates this
8# particular file as subject to the "Classpath" exception as provided
9# by Oracle in the LICENSE file that accompanied this code.
10#
11# This code is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# version 2 for more details (a copy is included in the LICENSE file that
15# accompanied this code).
16#
17# You should have received a copy of the GNU General Public License version
18# 2 along with this work; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22# or visit www.oracle.com if you need additional information or have any
23# questions.
24#
25
26################################################################################
27# Setup the standard C/C++ runtime libraries.
28#
29# Most importantly, determine if stdc++ should be linked statically or
30# dynamically.
31################################################################################
32AC_DEFUN_ONCE([LIB_SETUP_STD_LIBS],
33[
34  # statically link libstdc++ before C++ ABI is stablized on Linux unless
35  # dynamic build is configured on command line.
36  AC_ARG_WITH([stdc++lib], [AS_HELP_STRING([--with-stdc++lib=<static>,<dynamic>,<default>],
37      [force linking of the C++ runtime on Linux to either static or dynamic, default is static with dynamic as fallback])],
38      [
39        if test "x$with_stdc__lib" != xdynamic && test "x$with_stdc__lib" != xstatic \
40                && test "x$with_stdc__lib" != xdefault; then
41          AC_MSG_ERROR([Bad parameter value --with-stdc++lib=$with_stdc__lib!])
42        fi
43      ],
44      [with_stdc__lib=default]
45  )
46
47  if test "x$OPENJDK_TARGET_OS" = xlinux; then
48    # Test if -lstdc++ works.
49    AC_MSG_CHECKING([if dynamic link of stdc++ is possible])
50    AC_LANG_PUSH(C++)
51    OLD_CXXFLAGS="$CXXFLAGS"
52    CXXFLAGS="$CXXFLAGS -lstdc++"
53    AC_LINK_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
54        [has_dynamic_libstdcxx=yes],
55        [has_dynamic_libstdcxx=no])
56    CXXFLAGS="$OLD_CXXFLAGS"
57    AC_LANG_POP(C++)
58    AC_MSG_RESULT([$has_dynamic_libstdcxx])
59
60    # Test if stdc++ can be linked statically.
61    AC_MSG_CHECKING([if static link of stdc++ is possible])
62    STATIC_STDCXX_FLAGS="-Wl,-Bstatic -lstdc++ -lgcc -Wl,-Bdynamic"
63    AC_LANG_PUSH(C++)
64    OLD_LIBS="$LIBS"
65    OLD_CXX="$CXX"
66    LIBS="$STATIC_STDCXX_FLAGS"
67    CXX="$CC"
68    AC_LINK_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
69        [has_static_libstdcxx=yes],
70        [has_static_libstdcxx=no])
71    LIBS="$OLD_LIBS"
72    CXX="$OLD_CXX"
73    AC_LANG_POP(C++)
74    AC_MSG_RESULT([$has_static_libstdcxx])
75
76    if test "x$has_static_libstdcxx" = xno && test "x$has_dynamic_libstdcxx" = xno; then
77      AC_MSG_ERROR([Cannot link to stdc++, neither dynamically nor statically!])
78    fi
79
80    if test "x$with_stdc__lib" = xstatic && test "x$has_static_libstdcxx" = xno; then
81      AC_MSG_ERROR([Static linking of libstdc++ was not possible!])
82    fi
83
84    if test "x$with_stdc__lib" = xdynamic && test "x$has_dynamic_libstdcxx" = xno; then
85      AC_MSG_ERROR([Dynamic linking of libstdc++ was not possible!])
86    fi
87
88    # If dynamic was requested, it's available since it would fail above otherwise.
89    # If dynamic wasn't requested, go with static unless it isn't available.
90    AC_MSG_CHECKING([how to link with libstdc++])
91    if test "x$with_stdc__lib" = xdynamic || test "x$has_static_libstdcxx" = xno || HOTSPOT_CHECK_JVM_VARIANT(zeroshark); then
92      LIBCXX="$LIBCXX -lstdc++"
93      # To help comparisons with old build, put stdc++ first in JVM_LIBS
94      JVM_LIBS="-lstdc++ $JVM_LIBS"
95      # Ideally, we should test stdc++ for the BUILD toolchain separately. For now
96      # just use the same setting as for the TARGET toolchain.
97      OPENJDK_BUILD_JVM_LIBS="-lstdc++ $OPENJDK_BUILD_JVM_LIBS"
98      LDCXX="$CXX"
99      STATIC_CXX_SETTING="STATIC_CXX=false"
100      AC_MSG_RESULT([dynamic])
101    else
102      LIBCXX="$LIBCXX $STATIC_STDCXX_FLAGS"
103      JVM_LDFLAGS="$JVM_LDFLAGS -static-libgcc"
104      # To help comparisons with old build, put stdc++ first in JVM_LIBS
105      JVM_LIBS="-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic $JVM_LIBS"
106      # Ideally, we should test stdc++ for the BUILD toolchain separately. For now
107      # just use the same setting as for the TARGET toolchain.
108      OPENJDK_BUILD_JVM_LDFLAGS="$OPENJDK_BUILD_JVM_LDFLAGS -static-libgcc"
109      OPENJDK_BUILD_JVM_LIBS="-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic $OPENJDK_BUILD_JVM_LIBS"
110      LDCXX="$CC"
111      STATIC_CXX_SETTING="STATIC_CXX=true"
112      AC_MSG_RESULT([static])
113    fi
114  fi
115  AC_SUBST(STATIC_CXX_SETTING)
116
117  # libCrun is the c++ runtime-library with SunStudio (roughly the equivalent of gcc's libstdc++.so)
118  if test "x$TOOLCHAIN_TYPE" = xsolstudio && test "x$LIBCXX" = x; then
119    LIBCXX="${SYSROOT}/usr/lib${OPENJDK_TARGET_CPU_ISADIR}/libCrun.so.1"
120  fi
121
122  # TODO better (platform agnostic) test
123  if test "x$OPENJDK_TARGET_OS" = xmacosx && test "x$LIBCXX" = x && test "x$TOOLCHAIN_TYPE" = xgcc; then
124    LIBCXX="-lstdc++"
125  fi
126  AC_SUBST(LIBCXX)
127
128  # Setup Windows runtime dlls
129  if test "x$OPENJDK_TARGET_OS" = "xwindows"; then
130    TOOLCHAIN_SETUP_VS_RUNTIME_DLLS
131  fi
132])
133