2014-04-03 22 views
4

我想設置基於是否不平行版本是使一些變量,所以我嘗試這樣做:我能否告訴如果在Makefile中使用了--jobs?

jobs: 
»·echo "executing jobs job" 

ifneq (,$(findstring -j,$(MAKEFLAGS))) 
»·$(warning "parallel!") 
else 
»·$(warning "not parallel!") 
endif 

這是發生了什麼:

$ make -j2 
Makefile:2: "not parallel!" 
echo "executing jobs job" 
executing jobs job 

我也試過測試$(JOBS),但沒有運氣。

有沒有辦法讓我告訴Makefile裏面的--jobs參數被使用?


附加信息:

$ make --version 
GNU Make 3.81 
Copyright (C) 2006 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. 
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. 

This program built for x86_64-pc-linux-gnu 
+0

出於好奇,你爲什麼想要知道這一點? –

+0

構建當前做了一些漂亮的調試日誌記錄,並行執行時會中斷。我想根據構建是否並行完成來啓用/禁用此調試信息。例如。而不是'$(warning)',我會設置'enable-debug = 1'或'enable-debug = 0'。 – onionjake

+0

現在我很好奇你的漂亮調試日誌以及它如何中斷。另外如果你已經看到了make 3.82和4.0中的新輸出同步的東西。 –

回答

2

出人意料的是,${MAKEFLAGS}將只有當它在配方擴張的時間擴展獲得-j

的Makefile:

$(warning [${MAKEFLAGS}]) 

.PHONY: all 
all: 
    $(warning [${MAKEFLAGS}]) 
    echo Now do something useful 

運行:

$ make -j5 
1:1: [] 
1:5: [ -j --jobserver-fds=3,4] 
echo Now do something useful 
Now do something useful 
0

關於在MAKEFLAGS擴張@ bobbogo的answer:如果我們看一下代碼,我想我可以解釋的行爲:

展望在代碼中,main函數會多次調用define_makeflags函數。在main

/* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the 
    command switches. Include options with args if ALL is nonzero. 
    Don't include options with the 'no_makefile' flag set if MAKEFILE. */ 

static struct variable * 
define_makeflags (int all, int makefile) 
{ 
...... 

呼叫地點:

1)

/* Set up the MAKEFLAGS and MFLAGS variables for makefiles to see. 
    Initialize it to be exported but allow the makefile to reset it. */ 
define_makeflags (0, 0)->export = v_export; 

2)

/* Set up MAKEFLAGS and MFLAGS again, so they will be right. */ 

define_makeflags (1, 0); 

3)

/* Set up 'MAKEFLAGS' specially while remaking makefiles. */ 
define_makeflags (1, 1); 

在子函數中還有其他調用,但這應該足以解釋。

第一次調用all參數設置爲false。其他設置爲true。當all設置爲false時,define_makeflags函數僅解析「簡單標誌」,而j不是其中之一。爲了理解解析,需要查看此switch聲明和definition of command line params

我的贓物是這樣的:

我猜的ifneq語句解析第一次調用define_makeflags之後,但在後續調用之前發生。我可以猜到在開始時保持MAKEFLAGS簡單的原因是繼續支持文檔化的Makefile模式,如下所示。

doc1doc2

archive.a: ... 
ifneq (,$(findstring t,$(MAKEFLAGS))) 
     +touch archive.a 
     +ranlib -t archive.a 
else 
     ranlib archive.a 
endif 

如果MAKEFLAGS包含長選項或帶參數,然後在MAKEFLAGS對於單個字符標記搜索是不可能的選擇。

我的答案中有一些猜測。也許參與設計決策的人也可以參與其中。鑑於此,Paul Smith可能會有一個想法。

相關問題