顯示具有 Make 標籤的文章。 顯示所有文章
顯示具有 Make 標籤的文章。 顯示所有文章

2019/4/2

Transform recursive make to non-recursive make

工作上有個 repo 使用 recursive make 來 build,剛接手時因為不熟悉,就先照著他的流程使用。使用的過程中發現他有個缺點,需要在每個單獨 build 的 makefile 中寫出所有需要的 include path。照 GNU make 的說明是有方法可以把參數傳進 sub makefile 中的,不過這邊沒有這樣用,這邊使用的方式是把建立 parent path 的 makefile include 進所有的 sub makefile,在依照各個 lib 的需要自己處理 header path。這樣的做法會在各 sub makefile 中發現很多重複的 header path 設定。感覺很阿雜。

最近終於有點時間,project 沒有這麼趕了,就打算把這個問題修一下,之前有考慮過就傳進去就好,但是因為也想要可以使用 -j 來加速,以及改進下 output 的目錄結構來減少一些 maintain multiple core debugger script 的工作,就想改成 non-recursive 的結構順便練一下功。

主要碰到的問題有幾個,
  1. How to collect compile options in sub makefile?
  2. How to collect sources/headers in sub-directories?
  3. How to keep original build method? (build sub-dir as lib, then link them all)
在改的過程中有看到幾篇文章很有幫助,
[1] http://make.mad-scientist.net/papers/how-not-to-use-vpath/
[2] http://make.mad-scientist.net/papers/multi-architecture-builds/#single
[3] https://www.oreilly.com/library/view/managing-projects-with/0596006101/ch06.html#nonrecursive_make
[4] https://www.oreilly.com/library/view/managing-projects-with/0596006101/ch07.html#managing_programs_and_files

問題 1 & 2 的動作有兩個,一個是如何找到 makefile ,另一個是如何記住 makefile 中的資訊。

找檔案的手法有很多,這次是採用 wildcard 的方式,因為已經知道是在哪些目錄下了,這樣找也挺方便的。


再來就是如何記住,參考 [3] 的方法,這邊的重點是要記得先宣告參數,


之後在 sub makefile 中就可以把 source 或者是 compile option 累加進 var 中,


最後問題 3 的需求,主要的問題是如何跟 make 說有哪些 lib 要 build。這邊因為我是每個 sub makefile 都是一個 lib,所以就可以在每個 sub makefile 中加入下面的 rule,這樣每當 sub makefile 被 include 時,就會為每個 sub makefile 建立這個 rule 來 build 出我要的 lib。

2019/3/27

How Make do parallel build?

最近才知道 Make 有 "-j4" 這種可以做平行編譯的指令。再驚訝之餘,問題就來了,平常 compile 時都是一個個 source 在做,到底這個指令下去後,Make 是怎麼做的呢?想了解這後面的原理,感覺很厲害。

翻開 GNU Make manual,5.4 Parallel Execution 中說 make 就是知道如何平行的執行 makefile 中的 "recipe"
GNU make knows how to execute several recipes at once. Normally, make will execute only one recipe at a time, waiting for it to finish before executing the next. However, the ‘-j’ or ‘--jobs’ option tells make to execute many recipes simultaneously.

光這樣子看還是不太清楚這是在說什麼,從這個文章(https://www.cmcrossroads.com/article/pitfalls-and-benefits-gnu-make-parallelization)看就比較清楚,就是說 Make 可以同時執行多個彼此間沒有相依關係的動作。

Make 的基本形式中,"recipe" 是我們實際要讓 Make 做的動作。通常一個 target 的動作是有先後關係的,不過因為一般 makefile 中存在不只一個 target,不同 target 間就不一定有相依關係,這時 Make 就可以同時去執行這些不相依的 target 裡的動作。
target … : prerequisitesrecipe
        …
        …