working to patch CMake so that static builds (-static-intel) are possible

working to patch CMake so that static builds (-static-intel) are possible

Hi,

I am told that, for historical and compatibility reasons (since it is unknown whether a library, say -lfoo,is dynamic/shared or static) the Linux Intel Fortrancompiler build rule generated byCMakepasses the contents of $CMAKE_SHARED_LIBRARY_LINK_Fortran_FLAG toifortduring the linking step. Currently, this ends up adding -i_dynamic to the ifort link flags to build an executable binary, which will override any -static-intelflag passed by the user. I can't find any documentation of -i_dynamic in the Intel 13.x man pages, but it is my understanding that this is equivalent to the -shared-intel flag.

  1. Are -i_dynamic and -shared-intel equivalent? Is one more portable (for backwards compatibility) than the other?
  2. Does one need to link against the shared Intel runtime libraries to link against other shared libraries? Would it be safe to drop the -i_dynamic flag from the ifort linking flags when building a binary executable, even if it links to a shared library with something like -lfoo?

For your reference, here is how CMake sets up compilation for the Intel Fortran compiler on Linux currently:

#=============================================================================
# Copyright 2002-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
# This module is shared by multiple languages; use include blocker.
if(__LINUX_COMPILER_INTEL)
return()
endif()
set(__LINUX_COMPILER_INTEL 1)
if(NOT XIAR)
set(_intel_xiar_hints)
foreach(lang C CXX Fortran)
if(IS_ABSOLUTE "${CMAKE_${lang}_COMPILER}")
get_filename_component(_hint "${CMAKE_${lang}_COMPILER}" PATH)
list(APPEND _intel_xiar_hints ${_hint})
endif()
endforeach()
find_program(XIAR NAMES xiar HINTS ${_intel_xiar_hints})
mark_as_advanced(XIAR)
endif()
macro(__linux_compiler_intel lang)
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "-fPIC")
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "-fPIE")
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "-fPIC")
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS "-shared")
# We pass this for historical reasons. Projects may have
# executables that use dlopen but do not set ENABLE_EXPORTS.
set(CMAKE_SHARED_LIBRARY_LINK_${lang}_FLAGS "-rdynamic")
if(XIAR)
# INTERPROCEDURAL_OPTIMIZATION
set(CMAKE_${lang}_COMPILE_OPTIONS_IPO -ipo)
set(CMAKE_${lang}_CREATE_STATIC_LIBRARY_IPO
"${XIAR} cr <TARGET> <LINK_FLAGS> <OBJECTS> "
"${XIAR} -s <TARGET> ")
endif()
endmacro()

And then for Fortran, this macro is called:

include(Platform/Linux-Intel)
__linux_compiler_intel(Fortran)
set(CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS} -i_dynamic -nofor_main")
set(CMAKE_SHARED_LIBRARY_LINK_Fortran_FLAGS "-i_dynamic")

And here is the license:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
------------------------------------------------------------------------------
The above copyright and license notice applies to distributions of
CMake in source and binary form. Some source files contain additional
notices of original copyright by their contributors; see each source
for details. Third-party software packages supplied with CMake under
compatible licenses provide their own copyright notices documented in
corresponding subdirectories.
------------------------------------------------------------------------------
CMake was initially developed by Kitware with the following sponsorship:
* National Library of Medicine at the National Institutes of Health
as part of the Insight Segmentation and Registration Toolkit (ITK).
* US National Labs (Los Alamos, Livermore, Sandia) ASC Parallel
Visualization Initiative.
* National Alliance for Medical Image Computing (NAMIC) is funded by the
National Institutes of Health through the NIH Roadmap for Medical Research,
Grant U54 EB005149.
* Kitware, Inc.

-Zaak
4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Best Reply

Regarding 1:  Repeating some from the reply to the later post: http://software.intel.com/en-us/forums/topic/486830 . -i_dynamic (the underscore form) was an option spelling used many many releases ago that was later mapped to -i-dynamic (the hyphen form) and then that form was depreciated in more current releases and it now maps to -shared-intel

Adding to that, for backwards compatibility, both the “-I” forms are officially depreciated which means recognition of them could be dropped in any future release. Our driver mostly silently ignores options it does not recognize, so that’s something to be aware of in choosing the option used here.

In terms of overriding one another, when both options appear the right-most option “wins”.

$ ifort -V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.1.106 Build 20131008

$ ifort -shared -o libfoo.so t1.f90 -fpic -static-intel -shared-intel
ifort: command line warning #10121: overriding '-static-intel' with '-shared-intel'

$ ifort -shared -o libfoo.so t1.f90 -fpic -shared-intel -static-intel
ifort: command line warning #10121: overriding '-shared-intel' with '-static-intel'

$ ifort -shared -o libfoo.so t1.f90 -fpic -static-intel -i_dynamic
ifort: command line warning #10121: overriding '-static-intel' with '-i_dynamic'

$ ifort -shared -o libfoo.so t1.f90 -fpic -static-intel -i-dynamic
ifort: command line warning #10121: overriding '-static-intel' with '-i-dynamic'

It appears the macro cited builds a command-line where -i_dynamic appears after user supplied options; therefore, -i_dynamic (currently) would override a user specified counter option of -static-intel.

Regarding 2:  When using -shared (to build a dynamic shared library) -shared-intel is implied. For an executable, the default is -static-intel. It is not required that one link shared Intel libraries to an executable that links to a shared library. The executable can be built using the default -static-intel and the shared library it links (e.g. libfoo) can be built (and will be by default) to shared Intel libraries.  Linking the executable to shared libraries creates a smaller executable and assuming the executable and shared lib it links depend on common shared libraries, that helps exploit more benefits of the dynamic linking usage model too.

Amazingly thorough and detailed response, thank you so much!

-Zaak

You're welcome. Thank for the kind words & Best-reply.

Leave a Comment

Please sign in to add a comment. Not a member? Join today