2012-01-17 21 views
6

Có cách nào để viết bình luận "tiêu chuẩn" trong Makefile để sau này đưa chúng vào một chương trình giống như Doxygen để tạo ra một tài liệu hay (HTML hay man) chẳng hạn? Tôi muốn có một cái nhìn tổng quan rõ ràng về các mục tiêu chính của tôi ở đâu đó nhưng không có gì quá lạ mắt.Làm cách nào để ghi lại tệp makefile?

Trả lời

5

Một liên lạc tốt đẹp là cung cấp một mục tiêu giả mạo help để in tóm tắt các mục tiêu và tùy chọn. Từ hạt nhân Linux Makefile:

help: 
     @echo 'Cleaning targets:' 
     @echo ' clean   - Remove most generated files but keep the config and' 
     @echo '     enough build support to build external modules' 
     @echo ' mrproper  - Remove all generated files + config + various backup files' 
     @echo ' distclean  - mrproper + remove editor backup and patch files' 
     @echo '' 
     @echo 'Configuration targets:' 
     @$(MAKE) -f $(srctree)/scripts/kconfig/Makefile help 
     @echo '' 

Nó có thể là một chút công việc để duy trì các tài liệu theo cách này, nhưng tôi thấy nó độc đáo tách những gì là để dành cho "người sử dụng" so với những gì được thiết kế cho những người duy trì Makefile bản thân (ý kiến ​​nội tuyến).

+0

Có lẽ tôi không đủ kiên nhẫn nhưng tôi nghi ngờ có điều gì khác ngoài những gì bạn đã nói. Cảm ơn nhiều !! –

+0

Nếu bạn muốn một số loại công cụ tự động, hãy xem [make-help] (https://github.com/gibatronic/make-help). – gibatronic

1

Self-Documenting Makefiles (John Graham-Cumming, 2005) cho phép bạn viết trợ giúp theo từng quy tắc. Đây là một giải pháp nhẹ và rất tiện lợi, hoạt động ít nhất với GNU Make.

Đây là số slightly modified version của tôi (phần trợ giúp giúp sắp xếp danh sách quy tắc dài).

1

tôi đã thực hiện giải pháp của riêng tôi sử dụng một kịch bản Perl ngắn mà định dạng sự giúp đỡ như các công cụ GNU khác:

SCRIPT_VERSION=v1.0 
SCRIPT_AUTHOR=John Doe 

all:    ##@Build Build all the project 

clean:    ##@Cleaning Remove all intermediate objects 

mrproper: clean  ##@Cleaning Remove all output and interemediate objects 

HELP_FUN = \ 
    %help; while(<>){[email protected]{$$help{$$2//'options'}},[$$1,$$3] \ 
    if/^([\w-_]+)\s*:.*\#\#(?:@(\w+))?\s(.*)$$/}; \ 
    print"$$_:\n", map" $$_->[0]".(" "x(20-length($$_->[0])))."$$_->[1]\n",\ 
    @{$$help{$$_}},"\n" for keys %help; \ 

help: ##@Miscellaneous Show this help 
    @echo -e "Usage: make [target] ...\n" 
    @perl -e '$(HELP_FUN)' $(MAKEFILE_LIST) 
    @echo -e "Written by $(SCRIPT_AUTHOR), version $(SCRIPT_VERSION)" 
    @echo -e "Please report any bug or error to the author." 

Mà cho này:

$ make help 
Usage: make [target] ... 

Miscellaneous: 
    help    Show this help 

Build: 
    all     Build all the project 

Cleaning: 
    clean    Remove all intermediate objects 
    mrproper   Remove all output and interemediate objects 

Written by John Doe, version v1.0 
Please report any bug or error to the author. 
+1

Một chút cải tiến cho bit HELP_FUN: thay thế 'if/^ (\ w +) \ s *:' bằng 'if/^ ([\ w -_] +) \ s *:' để cho phép các mục tiêu của kiểu "this-target" hoặc "that_target". – th3n3rd

1

Sau đây là một giải pháp đơn giản mà không làm yêu cầu xác định chức năng người dùng hoặc tổng hợp văn bản trợ giúp cách xa các quy tắc mà họ ghi lại.

# This is a regular comment, that will not be displayed 

## ---------------------------------------------------------------------- 
## This is a help comment. The purpose of this Makefile is to demonstrate 
## a simple help mechanism that uses comments defined alongside the rules 
## they describe without the need of additional help files or echoing of 
## descriptions. Help comments are displayed in the order defined within 
## the Makefile. 
## ---------------------------------------------------------------------- 

help:  ## Show this help. 
    @sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST) 

build: ## Build something. 

install: ## Install something. 

deploy: ## Deploy something. 

format: ## Help comments are display with their leading whitespace. For 
      ## example, all comments in this snippet are aligned with spaces. 

Chạy make hoặc make help kết quả trong những điều sau đây:

---------------------------------------------------------------------- 
This is a help comment. The purpose of this Makefile is to demonstrate 
a simple help mechanism that uses comments defined alongside the rules 
they describe without the need of additional help files or echoing of 
descriptions. Help comments are displayed in the order defined within 
the Makefile. 
---------------------------------------------------------------------- 
help:  Show this help. 
build: Build something. 
install: Install something. 
deploy: Deploy something. 
format: Help comments are display with their leading whitespace. For 
      example, all comments in this snippet are aligned with spaces. 
Các vấn đề liên quan