解决mac下transport-native-unix-common编译失败问题

Mac 下编译 netty 报错,提示 Netty/Transport/Native/Unix/Common 模块编译失败,到网上搜索一下,并未发现有人遇到过类似问题,因此做下记录。

问题排查

Netty 源码来自 https://github.com/netty/netty.git,使用的是 4.1 分支

编译使用的命令是 mvn clean install -Dmaven.test.skip

编译结果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[INFO] Reactor Summary for Netty 4.1.59.Final-SNAPSHOT:
[INFO]
[INFO] Netty/Dev-Tools .................................... SUCCESS [ 0.702 s]
[INFO] Netty .............................................. SUCCESS [ 7.312 s]
[INFO] Netty/Common ....................................... SUCCESS [ 12.437 s]
[INFO] Netty/Buffer ....................................... SUCCESS [ 6.144 s]
[INFO] Netty/Resolver ..................................... SUCCESS [ 2.562 s]
[INFO] Netty/Transport .................................... SUCCESS [ 5.945 s]
[INFO] Netty/Codec ........................................ SUCCESS [ 6.889 s]
[INFO] Netty/Codec/DNS .................................... SUCCESS [ 2.848 s]
[INFO] Netty/Codec/HAProxy ................................ SUCCESS [ 2.426 s]
[INFO] Netty/Handler ...................................... SUCCESS [ 7.069 s]
[INFO] Netty/Codec/HTTP ................................... SUCCESS [ 7.346 s]
[INFO] Netty/Codec/HTTP2 .................................. SUCCESS [ 6.570 s]
[INFO] Netty/Codec/Memcache ............................... SUCCESS [ 2.425 s]
[INFO] Netty/Codec/MQTT ................................... SUCCESS [ 2.799 s]
[INFO] Netty/Codec/Redis .................................. SUCCESS [ 2.397 s]
[INFO] Netty/Codec/SMTP ................................... SUCCESS [ 2.103 s]
[INFO] Netty/Codec/Socks .................................. SUCCESS [ 2.930 s]
[INFO] Netty/Codec/Stomp .................................. SUCCESS [ 2.212 s]
[INFO] Netty/Codec/XML .................................... SUCCESS [ 2.235 s]
[INFO] Netty/Handler/Proxy ................................ SUCCESS [ 2.279 s]
[INFO] Netty/Resolver/DNS ................................. SUCCESS [ 3.554 s]
[INFO] Netty/Transport/RXTX ............................... SUCCESS [ 1.993 s]
[INFO] Netty/Transport/SCTP ............................... SUCCESS [ 2.579 s]
[INFO] Netty/Transport/UDT ................................ SUCCESS [ 2.602 s]
[INFO] Netty/Example ...................................... SUCCESS [ 5.584 s]
[INFO] Netty/Transport/Native/Unix/Common ................. FAILURE [ 0.245 s]
[INFO] Netty/Testsuite .................................... SKIPPED
[INFO] Netty/Transport/Native/Unix/Common/Tests ........... SKIPPED
[INFO] Netty/Transport/Native/KQueue ...................... SKIPPED
[INFO] Netty/Resolver/DNS/MacOS ........................... SKIPPED
[INFO] Netty/Transport/Native/Epoll ....................... SKIPPED
[INFO] Netty/All-in-One ................................... SKIPPED
[INFO] Netty/Tarball ...................................... SKIPPED
[INFO] Netty/Testsuite/Autobahn ........................... SKIPPED
[INFO] Netty/Testsuite/Http2 .............................. SKIPPED
[INFO] Netty/Testsuite/OSGI ............................... SKIPPED
[INFO] Netty/Testsuite/Shading ............................ SKIPPED
[INFO] Netty/Testsuite/Native ............................. SKIPPED
[INFO] Netty/Testsuite/NativeImage ........................ SKIPPED
[INFO] Netty/Testsuite/NativeImage/Client ................. SKIPPED
[INFO] Netty/Testsuite/NativeImage/ClientRuntimeInit ...... SKIPPED
[INFO] Netty/Transport/BlockHound/Tests ................... SKIPPED
[INFO] Netty/Microbench ................................... SKIPPED
[INFO] Netty/BOM .......................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

很明显是 Netty/Transport/Native/Unix/Common 编译未通过,查看后面的报错提示

1
2
3
4
5
6
7
8
9
10
11
12
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (build-native-lib) on project netty-transport-native-unix-common: An Ant BuildException has occured: exec returned: 1
[ERROR] around Ant part ...<exec resolveexecutable="true" failonerror="true" executable="make">... @ 4:71 in /Users/gorden5566/github/netty/transport-native-unix-common/target/antrun/build-main.xml
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <args> -rf :netty-transport-native-unix-common

出现一个 Ant 构建异常,加上 -X 参数重新执行编译,命令如下

1
mvn clean install -Dmaven.test.skip=true -X

得到如下提示信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (build-native-lib) on project netty-transport-native-unix-common: An Ant BuildException has occured: exec returned: 1
[ERROR] around Ant part ...<exec resolveexecutable="true" failonerror="true" executable="make">... @ 4:71 in /Users/gorden5566/github/netty/transport-native-unix-common/target/antrun/build-main.xml
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (build-native-lib) on project netty-transport-native-unix-common: An Ant BuildException has occured: exec returned: 1
around Ant part ...<exec resolveexecutable="true" failonerror="true" executable="make">... @ 4:71 in /Users/gorden5566/github/netty/transport-native-unix-common/target/antrun/build-main.xml
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:60)
Caused by: org.apache.maven.plugin.MojoExecutionException: An Ant BuildException has occured: exec returned: 1
around Ant part ...<exec resolveexecutable="true" failonerror="true" executable="make">... @ 4:71 in /Users/gorden5566/github/netty/transport-native-unix-common/target/antrun/build-main.xml
at org.apache.maven.plugin.antrun.AntRunMojo.execute (AntRunMojo.java:342)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:60)
Caused by: org.apache.tools.ant.BuildException: exec returned: 1
at org.apache.tools.ant.taskdefs.ExecTask.runExecute (ExecTask.java:646)
at org.apache.tools.ant.taskdefs.ExecTask.runExec (ExecTask.java:671)
at org.apache.tools.ant.taskdefs.ExecTask.execute (ExecTask.java:497)
at org.apache.tools.ant.UnknownElement.execute (UnknownElement.java:293)
at sun.reflect.GeneratedMethodAccessor30.invoke (Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute (DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform (Task.java:352)
at org.apache.tools.ant.Target.execute (Target.java:437)
at org.apache.tools.ant.Target.performTasks (Target.java:458)
at org.apache.tools.ant.Project.executeSortedTargets (Project.java:1406)
at org.apache.tools.ant.Project.executeTarget (Project.java:1377)
at org.apache.maven.plugin.antrun.AntRunMojo.execute (AntRunMojo.java:313)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.apache.maven.wrapper.BootstrapMainStarter.start (BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute (WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main (MavenWrapperMain.java:60)
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <args> -rf :netty-transport-native-unix-common

有这样一行

1
Caused by: org.apache.tools.ant.BuildException: exec returned: 1

看上去是 Ant 命令安装有问题,但不知道是什么原因。往上翻下提示,看看有没有执行信息提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[INFO] Executing tasks
Build sequence for target(s) `main' is [main]
Complete build sequence is [main, ]

main:
[exec] Current OS is Mac OS X
[exec] Setting environment variable: CC=clang
[exec] Setting environment variable: AR=ar
[exec] Setting environment variable: LIB_DIR=/Users/gorden5566/github/netty/transport-native-unix-common/target/native-lib-only
[exec] Setting environment variable: OBJ_DIR=/Users/gorden5566/github/netty/transport-native-unix-common/target/native-objs-only
[exec] Setting environment variable: JNI_PLATFORM=darwin
[exec] Setting environment variable: CFLAGS=-O3 -Werror -Wno-attributes -fPIC -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden
[exec] Setting environment variable: LDFLAGS=-Wl,--no-as-needed -lrt
[exec] Setting environment variable: LIB_NAME=libnetty-unix-common
[exec] Setting environment variable: MACOSX_DEPLOYMENT_TARGET=10.2
[exec] Executing 'make'
[exec] The ' characters around the executable and arguments are
[exec] not part of the command.
Execute:Java13CommandLauncher: Executing 'make'
The ' characters around the executable and arguments are
not part of the command.
[exec] xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

发现是少了 /Library/Developer/CommandLineTools/usr/bin/xcrun

这个提示好熟悉,是还未安装 Xcode Comand-line Tools

执行如下命令下载并安装

1
xcode-select --install

然后重新编译,提示如下信息

1
2
3
4
5
6
7
8
9
main:
[exec] In file included from src/main/c/netty_unix.c:16:mkdir -p /Users/gorden5566/github/netty/transport-native-unix-common/target/native-objs-only
[exec]
[exec] src/main/c/netty_unix_jni.h:19:10: fatal error: 'jni.h' file not found
[exec] #include <jni.h>
[exec] ^~~~~~~clang -o /Users/gorden5566/github/netty/transport-native-unix-common/target/native-objs-only/netty_unix.o -c src/main/c/netty_unix.c -O3 -Werror -Wno-attributes -fPIC -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden -I/include -I/include/darwin -Itarget/netty-jni-util
[exec]
[exec] 1 error generated.
[exec] make: *** [/Users/gorden5566/github/netty/transport-native-unix-common/target/native-objs-only/netty_unix.o] Error 1

未找到 jni.h 头文件。

检查 JAVA_HOME,发现并未设置

1
2
➜  ~ echo $JAVA_HOME

添加 JAVA_HOME 变量后编译通过

总结

若 netty 编译失败,请检查以下各项是否正常

  1. Xcode Comand-line Tools

    新安装的 mac 系统,默认是未安装的,可执行 xcode-select --install 命令安装

  2. JAVA_HOME

    若未设置,会提示找不到 jni.h 头文件

  3. 安装必要的工具和 lib 库

    1
    brew install autoconf automake libtool
  4. 如果你是用的 Linux 系统,请参考以下命令安装依赖

    1
    2
    3
    4
    5
    6
    7
    # RHEL/CentOS/Fedora:
    sudo yum install autoconf automake libtool make tar \
    glibc-devel \
    libgcc.i686 glibc-devel.i686
    # Debian/Ubuntu:
    sudo apt-get install autoconf automake libtool make tar \
    gcc

更多信息请参考 Native transports