Cross compiling/zh TW: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Cross compiling/zh TW
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Contents | |
| 2 | + | ||
| 3 | + | * 1 前言 | |
| 4 | + | * 1.1 host和target運行於同樣的CPU | |
| 5 | + | * 1.2 host和target運行於不同的CPU | |
| 6 | + | * 1.3 彙編器和連接器 | |
| 7 | + | * 1.4 用於target的單元 | |
| 8 | + | * 1.5 配置 | |
| 9 | + | * 1.6 基本步驟 | |
| 10 | + | * 2 從Linux | |
| 11 | + | * 2.1 到Linux | |
| 12 | + | * 2.2 到Windows | |
| 13 | + | * 2.3 到Darwin或者macOS | |
| 14 | + | * 3 從Windows | |
| 15 | + | * 3.1 到Linux | |
| 16 | + | * 3.2 到GO32v2 | |
| 17 | + | * 4 從Darwin(macOS)i386 | |
| 18 | + | * 4.1 從i386到PowerPC | |
| 19 | + | * 4.2 到Windows和Linux(32位) | |
| 20 | + | * 5 Cross compile FAQ | |
| 21 | + | * 5.1 為什麼要交叉編譯? | |
| 22 | + | * 5.2 為什麼是從UNIX到Windows而不是反過來? | |
| 23 | + | * 5.3 我需要更多構建FreePascal的資訊。哪兒可以找到? | |
| 24 | + | ||
| 25 | + | # 前言 | |
| 26 | + | ||
| 27 | + | 這是一個為新手寫的簡短的引言。接下來的章節將說明如何設置你的系統以進行交叉編譯,這表示用一個電腦平臺編譯出用於不同平臺的可執行程式——例如,在Linux平臺上編譯出在win32平臺上的可執行程式(或者FreeBSD或Darwin等等)。既然這樣,這個運行編譯器的平臺通常被稱為“host”(上述的Linux就是個例子,在上述情況下,Linux就被稱為host),而那個你希望你編譯出的程式能得以運行的平臺就稱為“target”。Free Pascal是個編譯器,基本原理就是將源代碼翻譯成可執行檔(機器語言)。這些可執行檔通常也包含了指示作業系統如何開始執行這個檔的資訊(比如,入口點資訊,段資訊,重定位表等等)。再者可執行檔也可能提及特定系統上的APIs(應用程式介面)的資訊,那就是為什麼不同作業系統必須用不同實現的(但幾乎同樣的介面)運行時庫。因此,這些可執行檔是平臺相關的。Free Pascal本身並不需要什麼設置。它能夠為不同平臺編譯可執行檔。只需要告訴它要怎麼幹就行。 | |
| 28 | + | ||
| 29 | + | ## host和target運行於同樣的CPU | |
| 30 | + | ||
| 31 | + | FPC被設計以可以編譯出適用於某種CPU的機器語言(因為不同CPU需要不同的機器語言),它也知道所有的被支援的平臺(作業系統)上的,架構上的(CPU及其機器的體系結構)具體需要的,能用到的東西。這表示你可以用為運行於同一類CPU的不同程式使用同一個編譯器執行交叉編譯。 | |
| 32 | + | ||
| 33 | + | ## host和target運行於不同的CPU | |
| 34 | + | ||
| 35 | + | 如果你要編譯出一個運行於不同CPU上的可執行檔,你需要一個特殊的交叉編譯器,例如,運行於一種平臺上的編譯器,能夠翻譯出不同的適用於不同CPU上的機器語言(就FPC來說,這樣一個交叉編譯器同樣能夠為所有支持的平臺和CPU架構編譯出適用的可執行檔)。因此這個交叉編譯器通常存放於同一個目錄內作為本機的編譯器。你可以自己編譯出這樣一個交叉編譯器,或者你可以直接使用由FPC團隊提供的已經為一些平臺完成的發佈版(這些平臺通常用於移動平臺,像arm-linux或者arm-wince,因為它們通常不會被作為host平臺)。當你使用了 -P 參數後,FPC程式就會target CPU架構選擇正確的編譯器(本機編譯器或者交叉編譯器之一)。 | |
| 36 | + | ||
| 37 | + | ## 彙編器和連接器 | |
| 38 | + | ||
| 39 | + | 編譯器只是一部分,我們需要彙編器和鏈結器。FPC僅僅為某些平臺提供內嵌的彙編器和鏈結器,其他平臺需要外部工具以提供支援。通常,這些工具不能夠為不同平臺編譯出二進位檔。那就是為什麼我們必須為每一個不同target平臺使用不同的鏈結器‘ld’和彙編器‘as’。它們是binutils。 | |
| 40 | + | ||
| 41 | + | ## 用於target的單元 | |
| 42 | + | ||
| 43 | + | 裝好了交叉編譯工具後(這樣的一個工具需要FPC RTL和其他為相應target平臺編譯好的單元檔)。例如,每種不同的target平臺需要不同的system.ppu檔(系統單元)等等。這些單元檔要麼是你自己為了target平臺編譯出來的,要麼可能是官方為某個版本的FPC編譯並發行的。 | |
| 44 | + | ||
| 45 | + | ## 配置 | |
| 46 | + | ||
| 47 | + | 你要設置你的FPC的配置檔,以使得交叉編譯變得容易,你可以忘記所有無聊的細節問題。 對LCL(lazarus cpmponent library)也可以這樣做(如果你用Lazarus)。然後你就可以為不同的target編譯出Pascal程式了。編譯出的可執行檔就可以拷貝到一台運行target的機器上,或者在模擬器上運行(例如:wine,bochs,xbox,vmware等等)。 | |
| 48 | + | ||
| 49 | + | ## 基本步驟 | |
| 50 | + | ||
| 51 | + | 有一些交叉編譯時通常的步驟你必須做的: | |
| 52 | + | ||
| 53 | + | 1. 有一個可運行於你的host平臺的Free Pascal編譯器。 | |
| 54 | + | 2. 你需要Free Pascal的源代碼。 | |
| 55 | + | 3. 你要麼從源碼構建,要麼從別人那兒獲得運行你的host系統但卻是為target系統設計的cross-binutils可執行檔。 | |
| 56 | + | 4. 有時,你需要一些target上的檔。 | |
| 57 | + | ||
| 58 | + | # 從Linux | |
| 59 | + | ||
| 60 | + | ## 到Linux | |
| 61 | + | ||
| 62 | + | 事先說明,以下文字說的是在linux(x86_64)上編譯出linux(i386)程式。關於以運行於ARM的Linux為目標的編譯(例如:Zaurus)應該可以在[[Setup Cross Compile For ARM|設置編譯器為ARM編譯程序]]找到。 | |
| 63 | + | ||
| 64 | + | 如果你的64位的Linux發行版已經可以編譯32位元程式,那麼事情就好辦了。由於FPC的設計的緣故,你還得做兩件事。 | |
| 65 | + | ||
| 66 | + | * 首先檢查你是否擁有兩個檔:i386-linux-ld和i386-linux-as: | |
| 67 | + | ||
| 68 | + | ``` | |
| 69 | + | bash $ which i386-linux-ld | |
| 70 | + | ``` | |
| 71 | + | ``` | |
| 72 | + | bash $ which i386-linux-as | |
| 73 | + | ``` | |
| 74 | + | ||
| 75 | + | 如果你有這兩個檔,那麼你就可以跳過下面"Compile FPC"標題所說的內容。 | |
| 76 | + | ||
| 77 | + | 我沒有這兩個檔所以我做了一對腳本: | |
| 78 | + | ``` | |
| 79 | + | #!/bin/bash | |
| 80 | + | # name this file /usr/bin/i386-linux-ld | |
| 81 | + | ld -A elf32-i386 $@ | |
| 82 | + | ``` | |
| 83 | + | ``` | |
| 84 | + | #!/bin/bash | |
| 85 | + | # name this file /usr/bin/i386-linux-as | |
| 86 | + | as --32 $@ | |
| 87 | + | ``` | |
| 88 | + | ||
| 89 | + | * 使它們可以被執行: | |
| 90 | + | ||
| 91 | + | ``` | |
| 92 | + | bash $ chmod +x /usr/bin/i386-linux-as | |
| 93 | + | bash $ chmod +x /usr/bin/i386-linux-ld | |
| 94 | + | ``` | |
| 95 | + | ||
| 96 | + | * 編譯FPC: | |
| 97 | + | ||
| 98 | + | ``` | |
| 99 | + | bash $ make all CPU_TARGET=i386 | |
| 100 | + | ``` | |
| 101 | + | ||
| 102 | + | 然後: | |
| 103 | + | ``` | |
| 104 | + | bash $ su -c "make install CPU_TARGET=i386" | |
| 105 | + | ``` | |
| 106 | + | ||
| 107 | + | 就這樣了。之後如果必要,你還可以編輯你的 /etc/fpc.cfg 文件。 | |
| 108 | + | ||
| 109 | + | ## 到Windows | |
| 110 | + | ||
| 111 | + | 關於用Lazarus做交叉編譯的資訊你可以在[[Cross compiling for Windows under Linux|在Linux下為Win32交叉編譯]]找到。 | |
| 112 | + | ||
| 113 | + | 如果你在編譯FPC的2.1.1版本或者或更新的分支版本,你可以這樣做: | |
| 114 | + | ``` | |
| 115 | + | bash $ make all OS_TARGET=win32 CPU_TARGET=i386 | |
| 116 | + | ``` | |
| 117 | + | ||
| 118 | + | 然後 | |
| 119 | + | ``` | |
| 120 | + | bash $ su -c "make crossinstall OS_TARGET=win32 CPU_TARGET=i386" | |
| 121 | + | ``` | |
| 122 | + | ||
| 123 | + | 注意:為win64編譯的話,用的命令是: make all OS_TARGET=win64 CPU_TARGET=x86_64 | |
| 124 | + | ||
| 125 | + | 如此簡單的原因是因為內部的鏈結器已經包含這個版本的FPC。 | |
| 126 | + | ||
| 127 | + | ## 到Darwin或者macOS | |
| 128 | + | ||
| 129 | + | * 首先你需要擁有用於target平臺的binutils。從下面這個站點下載odcctools(使用CVS)。根據他們的指示安裝:<http://www.opendarwin.org/projects/odcctools/> | |
| 130 | + | * 你需要建一個虛假的根目錄,就像:$HOME/darwinroot至少從你的蘋果或Darwin(蘋果的開源版本)系統拷貝/System和/Frameworks/usr目錄(你可能必須拷貝更多東西)到$HOME/darwinroot | |
| 131 | + | * 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. | |
| 132 | + | * now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there. | |
| 133 | + | ||
| 134 | + | type: | |
| 135 | + | ``` | |
| 136 | + | $PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks) | |
| 137 | + | ``` | |
| 138 | + | ||
| 139 | + | type (iirc): | |
| 140 | + | ``` | |
| 141 | + | make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib" | |
| 142 | + | ``` | |
| 143 | + | ||
| 144 | + | if that succeded you can install it to whereever you want with: | |
| 145 | + | ``` | |
| 146 | + | make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc | |
| 147 | + | ``` | |
| 148 | + | ||
| 149 | + | 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 | |
| 150 | + | ||
| 151 | + | * configure your /etc/fpc.cfg file. | |
| 152 | + | ||
| 153 | + | add a section like this: | |
| 154 | + | ``` | |
| 155 | + | #IFDEF powerpc | |
| 156 | + | -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/ | |
| 157 | + | -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl | |
| 158 | + | -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/* | |
| 159 | + | -k-systemroot $HOME/darwin/cross | |
| 160 | + | #ENDIF | |
| 161 | + | ``` | |
| 162 | + | ||
| 163 | + | 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. | |
| 164 | + | ||
| 165 | + | I may have missed some things (or most everything) as it's been a while since I did this. | |
| 166 | + | ||
| 167 | + | ||
| 168 | + | ||
| 169 | + | # 從Windows | |
| 170 | + | ||
| 171 | + | ## 到Linux | |
| 172 | + | ||
| 173 | + | 這很簡單,你可以在[[buildfaq]]看到相關資訊。 | |
| 174 | + | ||
| 175 | + | ## 到GO32v2 | |
| 176 | + | ||
| 177 | + | 你可以在[[Cross-compilation from Win32 to GO32v2|從Win32到GO32v2的交叉編譯]]找到詳細資訊。 | |
| 178 | + | ||
| 179 | + | # 從Darwin(macOS)i386 | |
| 180 | + | ||
| 181 | + | ## 從i386到PowerPC | |
| 182 | + | ||
| 183 | + | * 交叉binutils | |
| 184 | + | ||
| 185 | + | 首先檢查你是否已經有了以下檔powerpc-darwin-ld和powerpc-darwin-as: | |
| 186 | + | ``` | |
| 187 | + | $ which powerpc-darwin-ld | |
| 188 | + | $ which powerpc-darwin-as | |
| 189 | + | ``` | |
| 190 | + | ||
| 191 | + | 如果你已經有了上述檔就可以跳過創建符號鏈結,直接跳到“編譯FPC”標題。 | |
| 192 | + | ||
| 193 | + | 事實上,“normal”binutils能夠使用,因為它們是通用的。因此簡單的符號鏈結就夠了: | |
| 194 | + | ``` | |
| 195 | + | $ sudo ln -s /usr/bin/as /usr/bin/powerpc-darwin-as | |
| 196 | + | $ sudo ln -s /usr/bin/ld /usr/bin/powerpc-darwin-ld | |
| 197 | + | ``` | |
| 198 | + | ||
| 199 | + | 這些符號鏈結可以存其他目錄,只要她們在你的$PATH(for example /sw/bin when installing through fink)環境變數中就可以了。 | |
| 200 | + | ||
| 201 | + | * 編譯FPC: | |
| 202 | + | ||
| 203 | + | ``` | |
| 204 | + | $ cd fpc/compiler | |
| 205 | + | $ make cycle CPU_TARGET=powerpc | |
| 206 | + | ``` | |
| 207 | + | ||
| 208 | + | 這樣就能創建一個PowerPC編譯器(fpc/compiler/ppcppc)和rtl的單元。 | |
| 209 | + | ||
| 210 | + | 事實上,創建PowerPC的binaries不需要交叉編譯器。使用Rosetta,powerpc fpc和ppcppc在IntelMacs上運行得很好。 | |
| 211 | + | ||
| 212 | + | 關於兩者的平行的安裝和使用需要更多的測試和文檔(希望你來做測試和寫些文檔吧)。 | |
| 213 | + | ||
| 214 | + | 如果缺少單元檔,檢查你的配置檔,$HOME/.fpc.cfg或者/etc/fpc.cfg或者/sw/etc/fpc.cfg You may have to add something like or where ever your units are located. | |
| 215 | + | ``` | |
| 216 | + | -Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/ | |
| 217 | + | -Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl | |
| 218 | + | -Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/* | |
| 219 | + | ``` | |
| 220 | + | ||
| 221 | + | Reminder: Universal binaries are created from the individual (i386 and powerpc) binaries using lipo. | |
| 222 | + | ||
| 223 | + | ## 到Windows和Linux(32位) | |
| 224 | + | ||
| 225 | + | 包管理器[fink](<http://finkproject.org/>) 已經包含為Windows和linux(32位)交叉編譯的包了. | |
| 226 | + | ||
| 227 | + | 用 | |
| 228 | + | ``` | |
| 229 | + | $ fink install fpc-win32 | |
| 230 | + | ``` | |
| 231 | + | ||
| 232 | + | 和 | |
| 233 | + | ``` | |
| 234 | + | $ fink install fpc-i386-linux | |
| 235 | + | ``` | |
| 236 | + | ||
| 237 | + | 安裝交叉編譯器。 | |
| 238 | + | ``` | |
| 239 | + | fpc-cross-arm-gba | |
| 240 | + | fpc-cross-arm-linux | |
| 241 | + | fpc-cross-arm-nds | |
| 242 | + | fpc-cross-arm-wince | |
| 243 | + | fpc-cross-arm-armv4t-embedded | |
| 244 | + | fpc-cross-arm-armv7m-embedded | |
| 245 | + | fpc-cross-i386-darwin | |
| 246 | + | fpc-cross-i386-freebsd | |
| 247 | + | fpc-cross-i386-go32v2 | |
| 248 | + | fpc-cross-i386-linux | |
| 249 | + | fpc-cross-i386-nativent | |
| 250 | + | fpc-cross-i386-netbsd | |
| 251 | + | fpc-cross-i386-solaris | |
| 252 | + | fpc-cross-i386-win32 | |
| 253 | + | fpc-cross-i386-wince | |
| 254 | + | fpc-cross-jvm-android | |
| 255 | + | fpc-cross-jvm-java | |
| 256 | + | fpc-cross-m68k-linux | |
| 257 | + | fpc-cross-mipsel-linux | |
| 258 | + | fpc-cross-powerpc-linux | |
| 259 | + | fpc-cross-sparc-linux | |
| 260 | + | fpc-cross-x86-64-dragonfly | |
| 261 | + | fpc-cross-x86-64-freebsd | |
| 262 | + | fpc-cross-x86-64-linux | |
| 263 | + | fpc-cross-x86-64-win64 | |
| 264 | + | ``` | |
| 265 | + | ||
| 266 | + | # Cross compile FAQ | |
| 267 | + | ||
| 268 | + | ## 為什麼要交叉編譯? | |
| 269 | + | ||
| 270 | + | 這樣你就用不著為一個 作業系統/系統架構 開發一個程式或為另一個 作業系統/系統架構 編譯而重啟或切換你的電腦。 | |
| 271 | + | ||
| 272 | + | ## 為什麼是從UNIX到Windows而不是反過來? | |
| 273 | + | ||
| 274 | + | 主要的原因是因為生成的binaries在其他系統上(另一個UNIX甚至是包括Linux發行版)是非常複雜的。 主要的問題是: | |
| 275 | + | ||
| 276 | + | 1. 你在host上需要庫。 | |
| 277 | + | 2. 沒有一套庫可以作為越來越複雜的,不斷變化的UNIX版本的最終版本(你很可能按需編譯出一套新的庫來)。 | |
| 278 | + | 3. 總是需要更多(linker)參數和設置,這些東西在通常情況下很難描述明白。 | |
| 279 | + | ||
| 280 | + | 如果你在這個警告後還是堅持這樣做(從Windows到UNIX),請看一下crossnotes document mentioned further,或者[[buildfaq]]。 | |
| 281 | + | ||
| 282 | + | ## 我需要更多構建FreePascal的資訊。哪兒可以找到? | |
| 283 | + | ||
| 284 | + | 有一個通用的pdf格式的FAQ關於如何構建和配置FPC: [[buildfaq]] | |
| 285 | + | ||
| 286 | + | --- | |
| 287 | + | *From the Free Pascal wiki, page "Cross compiling/zh TW", as the Internet Archive kept it on 2025-03-23. 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).* |
