Cross compiling/zh CN: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # 前言 | |
| 2 | 2 | ||
| 3 | 3 | 这是一个为新手写的简短的引言。接下来的章节将说明如何设置你的系统以进行交叉编译,这表示用一个计算机平台编译出用于不同平台的可执行程序——例如,在Linux平台上编译出在win32平台上的可执行程序(或者FreeBSD或Darwin等等)。既然这样,这个运行编译器的平台通常被称为“host”(上述的Linux就是个例子,在上述情况下,Linux就被称为host),而那个你希望你编译出的程序能得以运行的平台就称为“target”。Free Pascal是个编译器,基本原理就是将源代码翻译成可执行文件(机器语言)。这些可执行文件通常也包含了指示操作系统如何开始执行这个文件的信息(比如,入口点信息,段信息,重定位表等等)。再者可执行文件也可能提及特定系统上的APIs(应用程序接口)的信息,那就是为什么不同操作系统必须用不同实现的(但几乎同样的接口)运行时库。因此,这些可执行文件是平台相关的。Free Pascal本身并不需要什么设置。它能够为不同平台编译可执行文件。只需要告诉它要怎么干就行。 | |
| 4 | 4 | ||
| 5 | 5 | ## host和target运行于同样的CPU | |
| 6 | 6 | ||
| 7 | 7 | FPC被设计以可以编译出适用于某种CPU的机器语言(因为不同CPU需要不同的机器语言),它也知道所有的被支持的平台(操作系统)上的,架构上的(CPU及其机器的体系结构)具体需要的,能用到的东西。这表示你可以用为运行于同一类CPU的不同程序使用同一个编译器执行交叉编译。 | |
| 8 | 8 | ||
| 9 | 9 | ## host和target运行于不同的CPU | |
| 10 | 10 | ||
| 11 | 11 | 如果你要编译出一个运行于不同CPU上的可执行文件,你需要一个特殊的交叉编译器,例如,运行于一种平台上的编译器,能够翻译出不同的适用于不同CPU上的机器语言(就FPC来说,这样一个交叉编译器同样能够为所有支持的平台和CPU架构编译出适用的可执行文件)。因此这个交叉编译器通常存放于同一个目录内作为本机的编译器。你可以自己编译出这样一个交叉编译器,或者你可以直接使用由FPC团队提供的已经为一些平台完成的发布版(这些平台通常用于移动平台,像arm-linux或者arm-wince,因为它们通常不会被作为host平台)。当你使用了 -P 参数后,FPC程序就会调用 -P 指定的target CPU架构的的编译器(本机编译器或者交叉编译器之一)。 | |
| 12 | 12 | ||
| 13 | 13 | ## 汇编器和连接器 | |
| 14 | 14 | ||
| 15 | 15 | 编译器只是一部分,我们需要汇编器和链接器。FPC仅仅为某些平台提供内嵌的汇编器和链接器,其他平台需要外部工具以提供支持。通常,这些工具不能够为不同平台编译出二进制文件。那就是为什么我们必须为每一个不同target平台使用不同的链接器‘ld’和汇编器‘as’。它们是binutils。 | |
| 16 | 16 | ||
| 17 | 17 | ## 用于target的单元 | |
| 18 | 18 | ||
| 19 | 19 | 装好了交叉编译工具后(这样的一个工具需要FPC RTL和其他为相应target平台编译好的单元文件)。例如,每种不同的target平台需要不同的system.ppu文件(系统单元)等等。这些单元文件要么是你自己为了target平台编译出来的,要么可能是官方为某个版本的FPC编译并发行的。 | |
| 20 | 20 | ||
| 21 | 21 | ## 配置 | |
| 22 | 22 | ||
| 23 | 23 | 你要设置你的FPC的配置文件,以使得交叉编译变得容易,你可以忘记所有无聊的细节问题。 对LCL(lazarus cpmponent library)也可以这样做(如果你用Lazarus)。然后你就可以为不同的target编译出Pascal程序了。编译出的可执行文件就可以拷贝到一台运行target的机器上,或者在模拟器上运行(例如:wine,bochs,xbox,vmware等等)。 | |
| 24 | 24 | ||
| 25 | 25 | ## 基本步骤 | |
| 26 | 26 | ||
| 27 | 27 | 有一些交叉编译时通常的步骤你必须做的: | |
| 28 | 28 | ||
| 29 | 29 | 1. 有一个可运行于你的host平台的Free Pascal编译器。 | |
| 30 | 30 | 2. 你需要Free Pascal的源代码。 | |
| 31 | 31 | 3. 你要么从源码构建,要么从别人那儿获得运行你的host系统但却是为target系统设计的cross-binutils可执行文件。 | |
| 32 | 32 | 4. 有时,你需要一些target上的文件。 | |
| 33 | 33 | ||
| 34 | 34 | # 从Linux | |
| 35 | 35 | ||
| 36 | 36 | ## 到Linux | |
| 37 | 37 | ||
| 38 | 38 | 事先说明,以下文字说的是在linux(x86_64)上编译出linux(i386)程序。关于以运行于ARM的Linux为目标的编译(例如:Zaurus)应该可以在[[Setup Cross Compile For ARM|设置编译器为ARM编译程序]]找到。 | |
| 39 | 39 | ||
| 40 | 40 | 如果你的64位的Linux发行版已经可以编译32位程序,那么事情就好办了。由于FPC的设计的缘故,你还得做两件事。 | |
| 41 | 41 | ||
| 42 | 42 | - 首先检查你是否拥有两个文件:i386-linux-ld和i386-linux-as: | |
| 43 | 43 | ||
| 44 | - | ||
| 44 | + | ```text | |
| 45 | + | bash $ which i386-linux-ld | |
| 46 | + | ``` | |
| 45 | 47 | ||
| 46 | - | ||
| 48 | + | ```text | |
| 49 | + | bash $ which i386-linux-as | |
| 50 | + | ``` | |
| 47 | 51 | ||
| 48 | 52 | 如果你有这两个文件,那么你就可以跳过下面"Compile FPC"标题所说的内容。 | |
| 49 | 53 | ||
| 50 | 54 | 我没有这两个文件所以我做了一对脚本: | |
| 51 | 55 | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 56 | + | ```text | |
| 57 | + | #!/bin/bash | |
| 58 | + | # name this file /usr/bin/i386-linux-ld | |
| 59 | + | ld -A elf32-i386 $@ | |
| 60 | + | ``` | |
| 55 | 61 | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 62 | + | ```text | |
| 63 | + | #!/bin/bash | |
| 64 | + | # name this file /usr/bin/i386-linux-as | |
| 65 | + | as --32 $@ | |
| 66 | + | ``` | |
| 59 | 67 | ||
| 60 | 68 | - 使它们可以被执行: | |
| 61 | 69 | ||
| 62 | - | ||
| 63 | - | ||
| 70 | + | ```text | |
| 71 | + | bash $ chmod +x /usr/bin/i386-linux-as | |
| 72 | + | bash $ chmod +x /usr/bin/i386-linux-ld | |
| 73 | + | ``` | |
| 64 | 74 | ||
| 65 | 75 | - 编译FPC: | |
| 66 | 76 | ||
| 67 | - | ||
| 77 | + | ```text | |
| 78 | + | bash $ make all CPU_TARGET=i386 | |
| 79 | + | ``` | |
| 68 | 80 | ||
| 69 | 81 | 然后: | |
| 70 | 82 | ||
| 71 | - | ||
| 83 | + | ```text | |
| 84 | + | bash $ su -c "make install CPU_TARGET=i386" | |
| 85 | + | ``` | |
| 72 | 86 | ||
| 73 | 87 | 就这样了。之后如果必要,你还可以编辑你的 /etc/fpc.cfg 文件。 | |
| 74 | 88 | ||
| 75 | 89 | ## 到Windows | |
| 76 | 90 | ||
| 77 | 91 | 关于用Lazarus做交叉编译的信息你可以在[[Cross compiling for Windows under Linux|在Linux下为Win32交叉编译]]找到。 | |
| 78 | 92 | ||
| 79 | 93 | 如果你在编译FPC的2.1.1版本或者或更新的分支版本,你可以这样做: | |
| 80 | 94 | ||
| 81 | - | ||
| 95 | + | ```text | |
| 96 | + | bash $ make all OS_TARGET=win32 CPU_TARGET=i386 | |
| 97 | + | ``` | |
| 82 | 98 | ||
| 83 | 99 | 然后 | |
| 84 | 100 | ||
| 85 | - | ||
| 101 | + | ```text | |
| 102 | + | bash $ su -c "make crossinstall OS_TARGET=win32 CPU_TARGET=i386" | |
| 103 | + | ``` | |
| 86 | 104 | ||
| 87 | 105 | 注意:为win64编译的话,用的命令是: make all OS_TARGET=win64 CPU_TARGET=x86_64 | |
| 88 | 106 | ||
| 89 | 107 | 如此简单的原因是因为内部的链接器已经包含这个版本的FPC。 | |
| 90 | 108 | ||
| 91 | 109 | ## 到Darwin或者macOS | |
| 92 | 110 | ||
| 93 | 111 | - 首先你需要拥有用于target平台的binutils。从下面这个站点下载odcctools(使用CVS)。根据他们的指示安装:http://www.opendarwin.org/projects/odcctools/ | |
| 94 | 112 | - 你需要建一个虚假的根目录,就像:\$HOME/darwinroot至少从你的苹果或Darwin(苹果的开源版本)系统拷贝/System和/Frameworks/usr目录(你可能必须拷贝更多东西)到\$HOME/darwinroot | |
| 95 | 113 | - now that you have these files make a folder in \$HOME/darwinroot called cross. where ever you installed the odcctools you need to make links for the cross tools to be more fpc friendly. there are a bunch of files from odcc tools called powerpc-apple-darwin-* you need to make links (or rename them) so powerpc-apple-darwin-ld becomes powerpc-darwin-ld, do the same for *-ar and *-as. | |
| 96 | 114 | - now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there. | |
| 97 | 115 | ||
| 98 | 116 | type: | |
| 99 | 117 | ||
| 100 | - | ||
| 118 | + | ```text | |
| 119 | + | $PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks) | |
| 120 | + | ``` | |
| 101 | 121 | ||
| 102 | 122 | type (iirc): | |
| 103 | 123 | ||
| 104 | - | ||
| 124 | + | ```text | |
| 125 | + | make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib" | |
| 126 | + | ``` | |
| 105 | 127 | ||
| 106 | 128 | if that succeded you can install it to whereever you want with: | |
| 107 | 129 | ||
| 108 | - | ||
| 130 | + | ```text | |
| 131 | + | make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc | |
| 132 | + | ``` | |
| 109 | 133 | ||
| 110 | 134 | now copy the file ./compiler/ppccross somewhere you will be able to find it as it's the compiler you'll need to build powerpc programs | |
| 111 | 135 | ||
| 112 | 136 | - configure your /etc/fpc.cfg file. | |
| 113 | 137 | ||
| 114 | 138 | add a section like this: | |
| 115 | 139 | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 140 | + | ```text | |
| 141 | + | #IFDEF powerpc | |
| 142 | + | -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/ | |
| 143 | + | -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl | |
| 144 | + | -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/* | |
| 145 | + | -k-systemroot $HOME/darwin/cross | |
| 146 | + | #ENDIF | |
| 147 | + | ``` | |
| 122 | 148 | ||
| 123 | 149 | whenever you want to crosscompile you have to have ppccross and the symlinks to powerpc-darwin-* in the PATH and you should be able to just do ppccross someprogie.pas and it will create a darwin executable. | |
| 124 | 150 | ||
| 125 | 151 | I may have missed some things (or most everything) as it's been a while since I did this. | |
| 126 | 152 | ||
| 127 | 153 | # 从Windows | |
| 128 | 154 | ||
| 129 | 155 | ## 到Linux | |
| 130 | 156 | ||
| 131 | 157 | 这很简单,你可以在[[buildfaq]]看到相关信息。 | |
| 132 | 158 | ||
| 133 | 159 | ## 到GO32v2 | |
| 134 | 160 | ||
| 135 | 161 | 你可以在[[Cross-compilation from Win32 to GO32v2|从Win32到GO32v2的交叉编译]]找到详细信息。 | |
| 136 | 162 | ||
| 137 | 163 | # 从Darwin(macOS)i386 | |
| 138 | 164 | ||
| 139 | 165 | ## 从i386到PowerPC | |
| 140 | 166 | ||
| 141 | 167 | - 交叉binutils | |
| 142 | 168 | ||
| 143 | 169 | 首先检查你是否已经有了以下文件powerpc-darwin-ld和powerpc-darwin-as: | |
| 144 | 170 | ||
| 145 | - | ||
| 146 | - | ||
| 171 | + | ```text | |
| 172 | + | $ which powerpc-darwin-ld | |
| 173 | + | $ which powerpc-darwin-as | |
| 174 | + | ``` | |
| 147 | 175 | ||
| 148 | 176 | 如果你已经有了上述文件就可以跳过创建符号链接,直接跳到“编译FPC”标题。 | |
| 149 | 177 | ||
| 150 | 178 | 事实上,“normal”binutils能够使用,因为它们是通用的。因此简单的符号链接就够了: | |
| 151 | 179 | ||
| 152 | - | ||
| 153 | - | ||
| 180 | + | ```text | |
| 181 | + | $ sudo ln -s /usr/bin/as /usr/bin/powerpc-darwin-as | |
| 182 | + | $ sudo ln -s /usr/bin/ld /usr/bin/powerpc-darwin-ld | |
| 183 | + | ``` | |
| 154 | 184 | ||
| 155 | 185 | 这些符号链接可以存其他目录,只要她们在你的\$PATH(for example /sw/bin when installing through fink)环境变量中就可以了。 | |
| 156 | 186 | ||
| 157 | 187 | - 编译FPC: | |
| 158 | 188 | ||
| 159 | - | ||
| 160 | - | ||
| 189 | + | ```text | |
| 190 | + | $ cd fpc/compiler | |
| 191 | + | $ make cycle CPU_TARGET=powerpc | |
| 192 | + | ``` | |
| 161 | 193 | ||
| 162 | 194 | 这样就能创建一个PowerPC编译器(fpc/compiler/ppcppc)和rtl的单元。 | |
| 163 | 195 | ||
| 164 | 196 | 事实上,创建PowerPC的binaries不需要交叉编译器。使用Rosetta,powerpc fpc和ppcppc在IntelMacs上运行得很好。 | |
| 165 | 197 | ||
| 166 | 198 | 关于两者的平行的安装和使用需要更多的测试和文档(希望你来做测试和写些文档吧)。 | |
| 167 | 199 | ||
| 168 | 200 | 如果缺少单元文件,检查你的配置文件,\$HOME/.fpc.cfg或者/etc/fpc.cfg或者/sw/etc/fpc.cfg You may have to add something like or where ever your units are located. | |
| 169 | 201 | ||
| 170 | - | ||
| 171 | - | ||
| 172 | - | ||
| 202 | + | ```text | |
| 203 | + | -Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/ | |
| 204 | + | -Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl | |
| 205 | + | -Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/* | |
| 206 | + | ``` | |
| 173 | 207 | ||
| 174 | 208 | Reminder: Universal binaries are created from the individual (i386 and powerpc) binaries using lipo. | |
| 175 | 209 | ||
| 176 | 210 | ## 到Windows和Linux(32位) | |
| 177 | 211 | ||
| 178 | 212 | 包管理器[fink](http://finkproject.org/) 已经包含为Windows和linux(32位)交叉编译的包了. | |
| 179 | 213 | ||
| 180 | 214 | 用 | |
| 181 | 215 | ||
| 182 | - | ||
| 216 | + | ```text | |
| 217 | + | $ fink install fpc-win32 | |
| 218 | + | ``` | |
| 183 | 219 | ||
| 184 | 220 | 和 | |
| 185 | 221 | ||
| 186 | - | ||
| 222 | + | ```text | |
| 223 | + | $ fink install fpc-i386-linux | |
| 224 | + | ``` | |
| 187 | 225 | ||
| 188 | 226 | 安装交叉编译器。 | |
| 189 | 227 | ||
| 190 | 228 | ``` bash | |
| 191 | 229 | fpc-cross-arm-gba | |
| 192 | 230 | fpc-cross-arm-linux | |
| 193 | 231 | fpc-cross-arm-nds | |
| 194 | 232 | fpc-cross-arm-wince | |
| 195 | 233 | fpc-cross-arm-armv4t-embedded | |
| 196 | 234 | fpc-cross-arm-armv7m-embedded | |
| 197 | 235 | fpc-cross-i386-darwin | |
| 198 | 236 | fpc-cross-i386-freebsd | |
| 199 | 237 | fpc-cross-i386-go32v2 | |
| 200 | 238 | fpc-cross-i386-linux | |
| 201 | 239 | fpc-cross-i386-nativent | |
| 202 | 240 | fpc-cross-i386-netbsd | |
| 203 | 241 | fpc-cross-i386-solaris | |
| 204 | 242 | fpc-cross-i386-win32 | |
| 205 | 243 | fpc-cross-i386-wince | |
| 206 | 244 | fpc-cross-jvm-android | |
| 207 | 245 | fpc-cross-jvm-java | |
| 208 | 246 | fpc-cross-m68k-linux | |
| 209 | 247 | fpc-cross-mipsel-linux | |
| 210 | 248 | fpc-cross-powerpc-linux | |
| 211 | 249 | fpc-cross-sparc-linux | |
| 212 | 250 | fpc-cross-x86-64-dragonfly | |
| 213 | 251 | fpc-cross-x86-64-freebsd | |
| 214 | 252 | fpc-cross-x86-64-linux | |
| 215 | 253 | fpc-cross-x86-64-win64 | |
| 216 | 254 | ``` | |
| 217 | 255 | ||
| 218 | 256 | # Cross compile FAQ | |
| 219 | 257 | ||
| 220 | 258 | ## 为什么要交叉编译? | |
| 221 | 259 | ||
| 222 | 260 | 这样你就用不着为一个 操作系统/系统架构 开发一个程序或为另一个 操作系统/系统架构 编译而重启或切换你的电脑。 | |
| 223 | 261 | ||
| 224 | 262 | ## 为什么是从UNIX到Windows而不是反过来? | |
| 225 | 263 | ||
| 226 | 264 | 主要的原因是因为为其他系统生成 binaries(另一个UNIX甚至是包括Linux发行版)是非常复杂的。 主要的问题是: | |
| 227 | 265 | ||
| 228 | 266 | 1. 你在host上需要库文件。 | |
| 229 | 267 | 2. 没有一套库文件可以作为越来越复杂的,不断变化的UNIX版本的最终版本(你很可能要编译出一套新的适合的库来)。 | |
| 230 | 268 | 3. 总是需要更多(linker)参数和设置,这些东西在通常情况下很难描述明白。 | |
| 231 | 269 | ||
| 232 | 270 | 如果你在这个警告后还是坚持这样做(从Windows到UNIX),请看一下crossnotes document mentioned further,或者[[buildfaq]]。 | |
| 233 | 271 | ||
| 234 | 272 | ## 我需要更多构建FreePascal的信息。哪儿可以找到? | |
| 235 | 273 | ||
| 236 | 274 | 有一个通用的pdf格式的FAQ关于如何构建和配置FPC: [[buildfaq]] | |
| 237 | 275 | ||
| 238 | 276 | --- | |
| 239 | 277 | *From the Free Pascal wiki, page "Cross compiling/zh CN", as it stood on 2021-07-19 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
