Skip to content

cmake build fails under windows when linking too many object files #2295

Closed

Description

@mhenschel

I'm building our project under windows using the cmake toolchain file provided in the emscripten sdk with the mingw makefile generator. Compiling works but linking a static or shared library results in an error about the command line being too long ("Kommandozeile zu lang" in German). So I started to investigate:

The Problem

I can build the same project with another cross compile toolchain I created myself without such errors. So it's not a problem of cmake itself. Windows can handle command lines up to 32k without problems. It think the difference here is that emscripten uses bat files to call the compiler, linker and "binutils". I found this about the maximum command line length for cmd.exe:
http://support.microsoft.com/kb/830473/en

It doesn't mention newer windows versions but chances are that cmd.exe hasn't been changed much in that regard. I didn't find much information about how cmake handles this so I searched the sources and and platform dependent files provided by cmake:

  1. When doing nothing special in a toolchain file cmake will create default definitions for CMAKE_C/CXX_ARCHIVE_CREATE and CMAKE_C/CXX_ARCHIVE_APPEND containing calls to CMAKE_AR.
    (This didn't work in the emscripten toolchain because the definition of CMAKE_AR missed the CACHE keyword. I filed another bug for this:
    CMAKE_AR in cmake toolchain file doesn't work #2288)
  2. The makefile generator of cmake has a hard coded limit of 30000 for the command line:
    http://cmake.org/gitweb?p=cmake.git;a=blob;f=Source/cmMakefileLibraryTargetGenerator.cxx;h=7ac025687d950f496d49c9bc539a1475a3853674;hb=HEAD#l536
  3. When a toolchain file doesn't specify a special rule to build a static library and CMAKE_C/CXX_ARCHIVE_CREATE, CMAKE_C/CXX_ARCHIVE_APPEND are defined (wich happens automatically if the toolchain file doesn't do it) cmake will automatically split the command to create a library using the hard coded limit.

So I think the problem is that the hard coded limit for the command line length isn't right when using bat files to call all the tools.

Proposed Solution

I tried to find a solution. The obvious one would be not to use bat files but I don't know if that's so easy. So I looked for a local solution in the cmake toolchain file. The other obvious solution would be to use response files. Fortunately both cmake and clang support this. There are some statements in the toolchain file that look like someone tried to use this functionality before:

SET(CMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS 1)
SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_OBJECTS 1)
SET(CMAKE_C_USE_RESPONSE_FILE_FOR_INCLUDES 1)
SET(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 1)

set(CMAKE_C_RESPONSE_FILE_LINK_FLAG "@")
set(CMAKE_CXX_RESPONSE_FILE_LINK_FLAG "@")

As far as I can see these statements will not do anything right now. The standard makefile generator does read the CMAKE_XXX_USE_RESPONSE_FILE_FOR_XXX flags but doesn't enable response files as long as CMAKE_XXX_CREATE_ARCHIVE and CMAKE_XXX_APPEND_ARCHIVE commands are available and no custom build rule for static libraries is specified. So I added a build rule for static libraries:

set(CMAKE_START_TEMP_FILE "@")
set(CMAKE_C_RESPONSE_FILE_LINK_FLAG "@")
set(CMAKE_CXX_RESPONSE_FILE_LINK_FLAG "@")
set(CMAKE_C_CREATE_STATIC_LIBRARY "<CMAKE_AR> rc <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_CREATE_STATIC_LIBRARY "<CMAKE_AR> rc <TARGET> <LINK_FLAGS> <OBJECTS>")

This worked for me. I can successfully link the static library now. CMake creates a file with an rsp extension, fills it with the object files to link together and feeds the file into emar. I also removed all the archive create/append commands as they are not required anymore. IMHO the use of CMAKE_START_TEMP_FILE in these commands is wrong anyway.

#set(CMAKE_CXX_ARCHIVE_CREATE "${CMAKE_AR} rc <TARGET> ${CMAKE_START_TEMP_FILE} <LINK_FLAGS> <OBJECTS>${CMAKE_END_TEMP_FILE}")
#set(CMAKE_C_ARCHIVE_CREATE "${CMAKE_AR} rc <TARGET> ${CMAKE_START_TEMP_FILE} <LINK_FLAGS> <OBJECTS>${CMAKE_END_TEMP_FILE}")
#set(CMAKE_CXX_ARCHIVE_APPEND "${CMAKE_AR} r <TARGET> ${CMAKE_START_TEMP_FILE} <LINK_FLAGS> <OBJECTS>${CMAKE_END_TEMP_FILE}")
#set(CMAKE_C_ARCHIVE_APPEND "${CMAKE_AR} r <TARGET> ${CMAKE_START_TEMP_FILE} <LINK_FLAGS> <OBJECTS>${CMAKE_END_TEMP_FILE}")

I'm still not finished with compiling all of our code. I'm not sure if other build steps like creating a shared library or creating an "executable" also correctly support response files. Maybe more changes to the toolchain file will be required. I would like to submit a patch when I'm done but I wanted to dump all that information here as long as I can still remember it ;-)

Activity

  1. mhenschel commented on May 16, 2014

    @mhenschel
    ContributorAuthor

    I made a pull request for my fix:
    #2359

  2. juj commented on Jul 25, 2014

    @juj
    Collaborator

    I think this was fixed by your pull request, let me close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions