如何将Autotools项目转换为CMake项目? [英] How do I convert an Autotools project to a CMake project?

查看:575
本文介绍了如何将Autotools项目转换为CMake项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,关于Autotools与CMake的话题似乎很多,但是对于我一生来说,我似乎找不到关于如何从Autotools转换项目的好教程(Makefile.amconfigure.ac文件)到CMake(CMakeLists.txt文件).如何做到这一点?

So there seems to be a lot of writing on the subject of Autotools vs. CMake, but for the life of me I can't seem to find a good tutorial on how to convert a project from Autotools (Makefile.am and configure.ac files) to CMake (CMakeLists.txt files). How does one go about doing this?

推荐答案

没有自动工具可以将任意构建系统从自动工具转换为cmake.我已经手动转换了一些中等规模的项目.我想为每组configure.ac和/或Makefile.am文件创建一个CMakeLists.txt文件,以简化从自动工具到cmake的转换.

There is no automated tool that will convert an arbitrary build system from autotools to cmake. I have converted a few moderately large projects manually. I like to create one CMakeLists.txt file for each set of configure.ac and/or Makefile.am file, to ease the conversion from autotools to cmake.

遗憾的是,这种手动方法需要在两种系统中发展专业知识,任何无辜者都不应施加任何一种经验.

Sadly, this manual approach requires developing an expertise in two systems, no one of which should be inflicted on any innocent.

  1. 捕获自动构建的工具在计算机上实际执行的记录.这将帮助您验证CMake构建系统是否使用相同的编译标志实际编译了每个文件,并创建了自动工具执行的每个库和可执行文件.至少在您的计算机上具有您的选择.

  1. Capture a record of what autotools build actually does on your machine. This will help you verify that your CMake build system actually compiles every file, using the same compile flags, and creates every library and executable that autotools does; at least on your machine, with your options.

%./configure [配置选项]> configure_autotools.log

% ./configure [configure options] > configure_autotools.log

%make> make_autotools.log

% make > make_autotools.log

%make install> make_install_autotools.log

% make install > make_install_autotools.log

  • 在包含configure.acMakefile.am文件的每个目录(包括项目根目录)中,创建一个新的空CMakeLists.txt文件.将cmake add_subdirectory(...)命令添加到每个非叶CMakeLists.txt文件中,以树状结构将所有cmake文件链接在一起.将cmake project()和cmake_minimum_required()命令添加到顶级CMakeLists.txt文件的顶部.

  • In every directory that contains a configure.ac or Makefile.am file, including the project root directory, create a new empty CMakeLists.txt file. Add cmake add_subdirectory(...) commands to each non-leaf CMakeLists.txt file to link all the cmake files together in a tree structure. Add cmake project() and cmake_minimum_required() commands to the top of your top level CMakeLists.txt file.

    project(MyProject)
    cmake_minimum_required(VERSION 2.8)
    add_subdirectory(foo)
    

  • 您现在有了一个一致但无用的cmake构建系统,该系统什么也不做.在更深的CMakeLists.txt文件中临时添加一些message(...)命令,以验证是否正确连接了所有内容.在调试cmake配置问题时,请记住message()是您的朋友.立即尝试构建,以确保您的cmake基础结构正常运行.什么都不会建造或安装;但是此时使用cmake基础架构很有价值.

  • You now have a consistent but useless cmake build system that does nothing. Temporarily add a few message(...) commands in the deeper CMakeLists.txt files to verify that everything is connected correctly. Remember that message() is your friend when debugging cmake configuration problems. Make sure your cmake infrastructure works by trying a build now. Nothing will get built or installed; but it is valuable to exercise the cmake infrastructure at this point.

    %mkdir build_cmake

    % mkdir build_cmake

    %cd build_cmake

    % cd build_cmake

    %ccmake -i [您的项目根目录]

    % ccmake -i [your project root]

    [配置,配置,生成]

    [configure, configure, generate]

    %make VERBOSE = 1

    % make VERBOSE=1

    %进行安装

  • 这是困难的部分.一张一张地填充您的CMakeLists.txt文件.以下是一些准则:

  • This is the hard part. One by one, populate your CMakeLists.txt files. Here are some guidelines:

    • 打开两个并排编辑器,一个带有CMakeLists.txt文件,另一个带有相应的Makefile.am/configure.ac文件.

    • Open two side-by-side editors, one with the CMakeLists.txt file, the other with the corresponding Makefile.am/configure.ac file.

    将浏览器打开至 cmake命令文档.

    如果可以在项目(cmake add_library()add_executable())中找到一个库或可执行文件,则可以从创建cmake规则开始,而不是从头开始.使用这种方法,您可以一次建立一个转换.

    Rather than starting from the very top, start off by creating cmake rules for building a small library or executable, if you can find one in the project (cmake add_library() or add_executable()). Using this approach you can build up your conversion one piece at a time.

    在添加每个目标时,请定期测试您的cmake构建系统.

    Keep periodically testing your cmake build system as you add each target.

    我喜欢使用cmake file(GLOB ...)命令来紧凑地创建源文件列表.请注意,这是一个有争议的观点.

    I like to use the cmake file(GLOB ...) command to compactly create lists of source files. Be aware that this is a controversial opinion.

    许多常见的自动工具节都有相应的 CMake命令.例如,可以将autoconf AC_TRY_COMPILE 转换为cmake TRY_COMPILE .但是大多数情况下,我发现我必须了解每个小工具节的功能,并弄清楚如何编写相应的cmake.另外,还有一个一系列CMake模块,可帮助创建类似自动工具的config.h文件. . Makefile.am文件通常更易于转换,并且在转换为cmake之后实际上可以变得更紧凑.

    Many common autotools stanzas have corresponding CMake commands. For example autoconf AC_TRY_COMPILE can be converted to cmake TRY_COMPILE. But mostly I find I have to understand what every little autotools stanza does and figure out how to write the corresponding cmake. Also, there is a series of CMake modules that help create autotools-like config.h files. Makefile.am files are often easier to convert, and can actually become more compact after translation to cmake.

    将cmake构建结果与您在步骤1中捕获的自动工具日志进行比较.编译标志是否相同?是否建立了所有相同的目标?是否安装了相同的文件?

    Compare your cmake build results to the autotools logs you captured in step 1. Are the compile flags the same? Are all of the same targets built? Are the same files installed?

    不幸的是,最近我还没有将自动工具转换为cmake,因此我无法撰写全面的文章如果在Makefile.am中看到x,则在CMakeLists.txt中写入y".有人可以为公共Wiki建议一个合适的位置吗?在这个位置上,积极地从自动工具转换为cmake的人们可以积累全面的此类准则?我不确定此stackoverflow答案是否是此类列表的最佳位置...

    Unfortunately I have not converted autotools to cmake recently, so I am not in a position to write a comprehensive "if you see x in Makefile.am, write y in CMakeLists.txt". Can someone suggest a good location for a public wiki where folks who are actively converting from autotools to cmake can accumulate a comprehensive set of such guidelines? I'm not sure if this stackoverflow answer is the best place for such a list...

    要获得额外的荣誉,一旦一切正常,请调整cmake规则,以便能够从Windows Visual Studio生成和运行项目.尝试使用自动工具!

    For extra credit, once you have it all working, tweak your cmake rules to be able to build and run your project from Windows Visual Studio. Try that with autotools!

    这篇关于如何将Autotools项目转换为CMake项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆