
目录工具简介主要功能使用场景基本使用方法OpenHarmony 适配过程项目结构构建系统适配步骤遇到的问题及解决方案问题 1: Makefile 不存在问题 2: 变量名错误问题 3: 缺少 makeinfo 工具问题 4: Makefile 语法错误问题 5: amhello 示例构建失败构建脚本详解验证和测试总结工具简介GNU Automake是一个 Makefile 生成器旨在实现可移植性并符合 GNU 编码标准中的 Makefile 变量和目标规范。基本信息项目名称: GNU Automake版本: 1.18.1 (开发快照)官方网站: https://www.gnu.org/software/automake/许可证: GPL v2构建系统: Autotools (autoconf/automake)编程语言: Perl项目名称automake开源协议GPL-2.0-or-later源码版本1.18.1目标平台aarch64-linux-ohos依赖项OpenHarmony SDK、Autotools (autoconf, automake)操作系统平台OpenHarmony PC源码地址https://git.savannah.gnu.org/git/automake.git设计理念Automake 是一个 Perl 脚本输入文件是Makefile.am输出文件是Makefile.in这些文件用于 Autoconf。Automake 要求开发者在configure.ac中完成某些配置。Automake 的目标是可移植性生成的 Makefile 可以在多种 Unix 系统上工作符合标准遵循 GNU 编码标准自动化减少手动编写和维护 Makefile 的工作量功能丰富支持多种构建场景库、程序、测试、文档等主要功能Automake 的主要功能包括Makefile 生成从Makefile.am生成符合 GNU 标准的Makefile.in依赖跟踪自动处理源代码依赖关系安装规则自动生成安装和卸载规则分发支持支持make dist和make distcheck创建源代码分发包测试支持支持make check运行测试套件文档构建支持构建 info、HTML、PDF 等格式的文档库支持支持构建静态库和共享库交叉编译支持交叉编译到不同平台aclocal 工具Automake 包还包含aclocal程序用于根据configure.ac的内容生成aclocal.m4。它作为扩展和维护 autoconf 的机制允许其他包作者编写可以被 aclocal 自动使用的 m4 宏。使用场景Automake 在以下场景中特别有用GNU 软件包开发符合 GNU 编码标准的软件包跨平台项目需要在多种 Unix 系统上构建的项目复杂构建系统需要处理多个目录、库、程序和测试的项目开源项目需要标准化的构建和安装流程的项目构建系统现代化将旧的手写 Makefile 迁移到现代构建系统基本使用方法典型工作流程# 1. 从源代码仓库检出如果是开发版本gitclone https://git.savannah.gnu.org/git/automake.gitcdautomake# 2. 生成 configure 脚本开发版本需要./bootstrap# 3. 配置构建环境./configure--prefix/usr/local# 4. 编译make# 5. 运行测试可选makecheck# 6. 安装makeinstall基本命令# 生成 Makefile.inautomake --add-missing# 生成 aclocal.m4aclocal# 查看帮助automake--helpaclocal--help在项目中使用 Automake创建configure.ac定义项目配置创建Makefile.am定义构建规则运行aclocal生成aclocal.m4运行autoconf生成configure脚本运行automake生成Makefile.in运行./configure生成Makefile运行make编译项目OpenHarmony 适配过程项目结构automake/ ├── bootstrap # 引导脚本生成 configure ├── configure.ac # Autoconf 配置文件 ├── Makefile.am # Automake 输入文件 ├── bin/ # 二进制文件目录 │ ├── automake.in # automake Perl 脚本模板 │ └── aclocal.in # aclocal Perl 脚本模板 ├── lib/ # 库文件目录 │ ├── Automake/ # Automake Perl 模块 │ └── am/ # Makefile.am 片段 ├── doc/ # 文档目录 │ ├── automake.texi # Texinfo 文档源 │ └── amhello/ # 示例项目 ├── m4/ # m4 宏文件 ├── t/ # 测试套件 ├── build_ohos.sh # OpenHarmony 构建脚本 └── hnp.json # HNP 配置文件构建系统Automake 使用 Autotools 构建系统bootstrap生成configure脚本开发版本configure配置构建环境生成Makefilemake编译源代码和生成工具make install安装到指定目录适配步骤1. 创建构建脚本创建build_ohos.sh脚本配置 OpenHarmony 交叉编译环境#!/bin/bash# # build_ohos.sh - automake HarmonyOS 构建脚本# # automake 使用 autotools 构建系统# exportAUTOMAKE_INSTALL_HNP_PATH${HNP_PUBLIC_PATH}/automake.org/automake_1.0.0sys_prefix${PREFIX}exportPREFIX${AUTOMAKE_INSTALL_HNP_PATH}2. 生成 configure 脚本检查并生成configure脚本如果不存在# 生成 configure 脚本如果不存在if[!-fconfigure];thenif[-fbootstrap];then./bootstrap||{echoError: bootstrap failedexit1}elif[-fconfigure.ac];thenautoreconf-i||{echoError: autoreconf failedexit1}elseechoError: No configure script, bootstrap, or configure.ac foundexit1fifi3. 运行 configure配置构建环境使用aarch64-linux-gnu作为 host因为 autotools 不认识aarch64-linux-ohos./configure\--prefix${AUTOMAKE_INSTALL_HNP_PATH}\--hostaarch64-linux-gnu\--enable-static\--disable-shared\CC${CC}\CXX${CXX}\CFLAGS${CFLAGS}\CXXFLAGS${CXXFLAGS}\LDFLAGS${LDFLAGS}注意--enable-static和--disable-shared选项会被 configure 警告为未识别的选项但不影响构建。移植环境说明该命令基于build配置框架进行移植编译环境搭建可以参考文章《开源软件鸿蒙化适配与构建指南(交叉编译)》主要是ohos sdk的下载配置和build配置框架的下载使用说明。遇到的问题及解决方案问题 1: Makefile 不存在错误现象GNUmakefile:23: There seems to be no Makefile in this directory. GNUmakefile:24: You must run ./configure before running make. GNUmakefile:25: *** Fatal Error. Stop.问题分析Automake 使用 Autotools 构建系统需要先运行configure脚本生成Makefile。如果configure脚本不存在还需要先运行bootstrap或autoreconf生成它。解决方案在构建脚本中添加生成configure脚本的逻辑# 生成 configure 脚本如果不存在if[!-fconfigure];thenif[-fbootstrap];then./bootstrap||{echoError: bootstrap failedexit1}elif[-fconfigure.ac];thenautoreconf-i||{echoError: autoreconf failedexit1}fifi# 运行 configure./configure--prefix${AUTOMAKE_INSTALL_HNP_PATH}--hostaarch64-linux-gnu...问题 2: 变量名错误错误现象cp: /hnp.json: Read-only file system realpath unsuccess. path-o source dir path-o is invalid. tar: automake_1.0.0: Cannot stat: No such file or directory问题分析构建脚本中使用了错误的变量名TREE_INSTALL_HNP_PATH应该使用AUTOMAKE_INSTALL_HNP_PATH。同时缺少创建安装目录的步骤。解决方案修正变量名将所有TREE_INSTALL_HNP_PATH改为AUTOMAKE_INSTALL_HNP_PATH创建安装目录在 configure 之前创建安装目录# 创建安装目录mkdir-p${AUTOMAKE_INSTALL_HNP_PATH}||{echoError: Failed to create installation directoryexit1}问题 3: 缺少 makeinfo 工具错误现象/Users/jianguo/HarmonyOSPC/build/code/automake/lib/missing: line 85: makeinfo: command not found WARNING: makeinfo is missing on your system. make: *** [doc/automake-history.info] Error 127问题分析Automake 在构建时会尝试生成 info 格式的文档这需要makeinfo工具属于 Texinfo 包。在交叉编译场景中我们通常不需要构建文档因为文档不是运行时必需的交叉编译环境可能缺少文档构建工具文档可以在主机系统上单独构建解决方案跳过文档构建修改 Makefile 移除文档依赖# 跳过文档构建交叉编译时不需要文档且缺少 makeinfo# 设置 MAKEINFO 为空命令避免调用 makeinfoexportMAKEINFOtrue# 修改 Makefile 跳过文档目标if[-fMakefile];then# 使用 awk 更安全地处理多行 INFO_DEPS 变量定义awk /^INFO_DEPS / { print INFO_DEPS in_info_deps 1 next } in_info_deps (/^\t/ || /^[ \t]/) { # 跳过制表符或空格开头的续行Makefile 多行变量定义的续行 next } in_info_deps { # 遇到非续行结束 INFO_DEPS 定义 in_info_deps 0 } { print } MakefileMakefile.tmpmvMakefile.tmp Makefile# 移除 all-am 目标中的文档依赖sed-is/ $(INFO_DEPS)//gMakefile2/dev/null||truesed-is/ doc\/automake\.info doc\/automake-history\.info//gMakefile2/dev/null||truesed-is/ doc\/automake\.info//gMakefile2/dev/null||truesed-is/ doc\/automake-history\.info//gMakefile2/dev/null||true# 注释掉 doc 相关的 info 文件规则sed-is/^\(doc\/.*\.info:\)/#\1/gMakefile2/dev/null||truefi关键点使用awk安全地处理多行变量定义避免破坏 Makefile 语法清空INFO_DEPS变量移除所有文档依赖注释掉所有doc/*.info相关的构建规则问题 4: Makefile 语法错误错误现象Makefile:216: *** commands commence before first target. Stop.问题分析在使用sed修改INFO_DEPS变量时只替换了第一行留下了以制表符开头的续行导致 Makefile 语法错误。Makefile 中的多行变量定义格式如下INFO_DEPS \ $(srcdir)/doc/automake.info \ $(srcdir)/doc/automake-history.info如果只替换第一行会留下INFO_DEPS $(srcdir)/doc/automake-history.info这会导致 Makefile 解析错误因为制表符开头的行会被解释为命令但前面没有目标。解决方案使用awk安全地处理多行变量定义# 使用 awk 更安全地处理多行 INFO_DEPS 变量定义awk /^INFO_DEPS / { print INFO_DEPS in_info_deps 1 next } in_info_deps (/^\t/ || /^[ \t]/) { # 跳过制表符或空格开头的续行Makefile 多行变量定义的续行 next } in_info_deps { # 遇到非续行结束 INFO_DEPS 定义 in_info_deps 0 } { print } MakefileMakefile.tmpmvMakefile.tmp Makefile关键点识别INFO_DEPS 行跳过所有以制表符或空格开头的续行遇到非续行时结束处理将多行定义替换为单行空定义INFO_DEPS 问题 5: amhello 示例构建失败错误现象Wide character in print at ./doc/help2man line 668. Wide character in print at ./doc/help2man line 668. doc/amhello-1.0.tar.gz: recipe failed. See file /Users/jianguo/HarmonyOSPC/build/code/automake/doc/amhello/amhello-output.tmp for details make: *** [doc/amhello-1.0.tar.gz] Error 1问题分析amhello是 Automake 提供的示例项目用于演示如何使用 Automake。构建amhello-1.0.tar.gz时会调用help2man生成 man 页面但遇到了字符编码问题。在交叉编译场景中我们不需要构建示例项目。解决方案跳过amhello示例的构建# 注释掉 amhello-1.0.tar.gz 的构建规则sed-is|^\($(srcdir)/doc/amhello-1\.0\.tar\.gz:\)|#\1|gMakefile2/dev/null||true# 清空 dist_doc_DATA 变量移除 amhello-1.0.tar.gzsed-is|^dist_doc_DATA .*|dist_doc_DATA |Makefile2/dev/null||true# 从 DATA 变量中移除 dist_doc_DATA如果存在sed-is| $(dist_doc_DATA)||gMakefile2/dev/null||true# 注释掉 amhello 相关的规则避免构建示例文档sed-is|^\(doc/amhello.*:\)|#\1|gMakefile2/dev/null||true关键点注释掉amhello-1.0.tar.gz的构建规则清空dist_doc_DATA变量从DATA变量中移除dist_doc_DATA的引用构建脚本详解完整的build_ohos.sh脚本如下#!/bin/bash# # build_ohos.sh - automake HarmonyOS 构建脚本# # automake 使用 autotools 构建系统# exportAUTOMAKE_INSTALL_HNP_PATH${HNP_PUBLIC_PATH}/automake.org/automake_1.0.0sys_prefix${PREFIX}exportPREFIX${AUTOMAKE_INSTALL_HNP_PATH}echo${PREFIX}# 创建安装目录mkdir-p${AUTOMAKE_INSTALL_HNP_PATH}||{echoError: Failed to create installation directoryexit1}# 清理之前的配置如果存在if[-fMakefile];thenmakedistclean||truefirm-fconfig.status config.cache config.log# 生成 configure 脚本如果不存在if[!-fconfigure];thenif[-fbootstrap];then./bootstrap||{echoError: bootstrap failedexit1}elif[-fconfigure.ac];thenautoreconf-i||{echoError: autoreconf failedexit1}elseechoError: No configure script, bootstrap, or configure.ac foundexit1fifi# 运行 configure./configure\--prefix${AUTOMAKE_INSTALL_HNP_PATH}\--hostaarch64-linux-gnu\--enable-static\--disable-shared\CC${CC}\CXX${CXX}\CFLAGS${CFLAGS}\CXXFLAGS${CXXFLAGS}\LDFLAGS${LDFLAGS}||{echoError: configure failedexit1}# 跳过文档构建交叉编译时不需要文档且缺少 makeinfo# 设置 MAKEINFO 为空命令避免调用 makeinfoexportMAKEINFOtrue# 修改 Makefile 跳过文档目标if[-fMakefile];then# 使用 awk 更安全地处理多行 INFO_DEPS 变量定义awk /^INFO_DEPS / { print INFO_DEPS in_info_deps 1 next } in_info_deps (/^\t/ || /^[ \t]/) { # 跳过制表符或空格开头的续行Makefile 多行变量定义的续行 next } in_info_deps { # 遇到非续行结束 INFO_DEPS 定义 in_info_deps 0 } { print } MakefileMakefile.tmpmvMakefile.tmp Makefile# 移除 all-am 目标中的文档依赖sed-is/ $(INFO_DEPS)//gMakefile2/dev/null||truesed-is/ doc\/automake\.info doc\/automake-history\.info//gMakefile2/dev/null||truesed-is/ doc\/automake\.info//gMakefile2/dev/null||truesed-is/ doc\/automake-history\.info//gMakefile2/dev/null||true# 注释掉 doc 相关的 info 文件规则sed-is/^\(doc\/.*\.info:\)/#\1/gMakefile2/dev/null||true# 注释掉 amhello-1.0.tar.gz 的构建规则sed-is|^\($(srcdir)/doc/amhello-1\.0\.tar\.gz:\)|#\1|gMakefile2/dev/null||true# 清空 dist_doc_DATA 变量移除 amhello-1.0.tar.gzsed-is|^dist_doc_DATA .*|dist_doc_DATA |Makefile2/dev/null||true# 从 DATA 变量中移除 dist_doc_DATA如果存在sed-is| $(dist_doc_DATA)||gMakefile2/dev/null||true# 注释掉 amhello 相关的规则避免构建示例文档sed-is|^\(doc/amhello.*:\)|#\1|gMakefile2/dev/null||truefi# 构建跳过文档makeVERBOSE1-j$(sysctl-nhw.ncpu2/dev/null||echo4)||{echoError: make failedexit1}# 安装makeinstall||{echoError: make install failedexit1}# 复制 HNP 配置文件cphnp.json${AUTOMAKE_INSTALL_HNP_PATH}/||{echoError: failed to copy hnp.jsonexit1}# 打包if[-d${AUTOMAKE_INSTALL_HNP_PATH}];thenpushd${AUTOMAKE_INSTALL_HNP_PATH}/..//dev/null||{echoError: pushd failedexportPREFIX${sys_prefix}exit1}${HNP_TOOL}pack-i${AUTOMAKE_INSTALL_HNP_PATH}-o${ARCHIVE_PATH}/||{echoError: HNP pack failedpopd/dev/nullexportPREFIX${sys_prefix}exit1}tar-zvcf${ARCHIVE_PATH}/ohos_automake_1.0.0.tar.gz automake_1.0.0/||{echoError: tar failedpopd/dev/nullexportPREFIX${sys_prefix}exit1}popd/dev/nullelseechoError: Installation directory does not exist:${AUTOMAKE_INSTALL_HNP_PATH}exportPREFIX${sys_prefix}exit1fi# 恢复 PREFIXexportPREFIX${sys_prefix}echoBuild completed successfully!echoOutput files:echo -${ARCHIVE_PATH}/automake.hnpecho -${ARCHIVE_PATH}/ohos_automake_1.0.0.tar.gz脚本关键点说明环境变量设置设置安装路径和 PREFIX目录创建确保安装目录存在配置清理清理之前的构建配置configure 生成如果不存在通过bootstrap或autoreconf生成configure 运行配置交叉编译环境文档跳过使用awk和sed修改 Makefile 跳过文档构建构建和安装编译并安装到指定目录HNP 打包生成 HNP 格式包和 tar.gz 压缩包验证和测试构建验证构建成功后检查生成的文件# 检查 HNP 包ls-lhoutput/automake.hnp# 检查 tar.gz 包ls-lhoutput/ohos_automake_1.0.0.tar.gz# 检查安装目录ls-R${AUTOMAKE_INSTALL_HNP_PATH}功能测试在 OpenHarmony 设备上测试 automake# 检查版本automake--version# 检查帮助automake--help# 测试 aclocalaclocal--version使用示例在 OpenHarmony 项目中使用 automake# 1. 创建 Makefile.amcatMakefile.amEOF bin_PROGRAMS hello hello_SOURCES hello.c EOF# 2. 运行 automakeautomake --add-missing# 3. 运行 configure./configure# 4. 编译make总结适配要点Autotools 构建系统Automake 使用标准的 Autotools 构建流程需要先运行bootstrap或autoreconf生成configure脚本交叉编译配置使用--hostaarch64-linux-gnu作为目标平台autotools 不认识aarch64-linux-ohos文档构建跳过交叉编译时通常不需要构建文档需要设置MAKEINFOtrue避免调用 makeinfo使用awk安全地处理多行变量定义清空INFO_DEPS变量注释掉所有文档相关的构建规则示例项目跳过跳过amhello示例项目的构建避免字符编码问题Makefile 修改安全使用awk处理多行变量定义避免破坏 Makefile 语法适用场景Automake 的 OpenHarmony 适配适用于需要构建使用 Autotools 的项目开发符合 GNU 编码标准的软件包需要跨平台构建支持的项目构建系统现代化改造经验总结多行变量处理使用awk而不是sed处理 Makefile 中的多行变量定义避免语法错误文档构建交叉编译时跳过文档构建是常见做法可以避免工具依赖问题错误处理为所有关键步骤添加错误处理确保构建失败时能够及时发现问题变量命名使用项目特定的变量名如AUTOMAKE_INSTALL_HNP_PATH避免与其他项目冲突构建顺序确保正确的构建顺序清理 → 生成 configure → 运行 configure → 修改 Makefile → 构建 → 安装 → 打包参考资源Automake 官方网站GNU Autotools 文档OpenHarmony Native Package (HNP) 文档项目代码仓PC代码仓PC社区欢迎加入开源鸿蒙PC社区https://harmonypc.csdn.net/