Kernel.ParallelCompiler 모듈
Kernel.ParallelCompiler 모듈
Kernel.ParallelCompiler는 파일을 병렬로 컴파일하고 require하는 일을 책임지는 모듈이에요. 보통은 mix compile 같은 도구가 내부에서 쓰지만, 여러분의 빌드 흐름에서 파일들을 병렬로 컴파일해야 한다면 직접 쓸 수 있어요.
본문
병렬 컴파일 함수에 넘길 수 있는 옵션들을 정리한 타입은 다음과 같아요.
@type compile_opts() :: [
after_compile: (-> term()),
each_file: (Path.t() -> term()),
each_long_compilation: (Path.t() -> term()) | (Path.t(), pid() -> term()),
each_long_verification: (module() -> term()) | (module(), pid() -> term()),
each_module: (Path.t(), module(), binary() -> term()),
each_cycle: (-> {:compile, [module()], [Code.diagnostic(:warning)]}
| {:runtime, [module()], [Code.diagnostic(:warning)]}),
long_compilation_threshold: pos_integer(),
long_verification_threshold: pos_integer(),
verification: boolean(),
profile: :time,
dest: Path.t(),
beam_timestamp: term(),
return_diagnostics: boolean(),
max_concurrency: pos_integer(),
purge_compiler_modules: boolean()
]
compile/2
@spec compile([Path.t()], compile_opts()) ::
{:ok, [atom()], [warning()] | info()}
| {:error, [error()] | [Code.diagnostic(:error)], [warning()] | info()}
주어진 파일들을 컴파일해요. 이 파일들은 병렬로 컴파일되고, 파일 사이의 의존성을 자동으로 감지해요. 의존성이 발견되면 의존이 해결될 때까지 현재 파일의 컴파일을 멈춰요.
return_diagnostics: true 옵션을 넣어 호출해야 {:ok, modules, warnings_info} 또는 {:error, errors, warnings_info}를 돌려주는데, warnings_info는 다음과 같은 모양이에요.
%{
runtime_warnings: [warning],
compile_warnings: [warning]
}
주요 옵션은 다음과 같아요.
:after_compile— 모든 모듈이 컴파일된 뒤, 검증(verification) 전에 호출돼요.:each_file— 파일마다 컴파일되면 해당 파일을 인자로 콜백을 호출해요.:each_long_compilation—:long_compilation_threshold보다 오래 걸리는 파일마다 콜백 호출.:each_long_verification(v1.19.0+) —:long_verification_threshold보다 오래 걸리는 모듈마다 콜백 호출.:each_module— 모듈마다 파일·모듈·모듈 바이트코드를 인자로 콜백 호출.:each_cycle— 주어진 파일들을 컴파일한 뒤 호출돼서{:compile, modules, warnings}(계속 컴파일) 또는{:runtime, modules, warnings}(의존 모듈 변경으로 컴파일 중단 후 검증)을 반환해야 해요.:long_compilation_threshold— 파일이 오래 걸리는지 확인할 타임아웃(초). 기본 10초.:long_verification_threshold(v1.19.0+) — 모듈 검증이 오래 걸리는지 확인할 타임아웃(초). 기본 10초.:verification(v1.19.0+) — 미사용 함수·deprecation 경고·타입 검사 같은 코드 검증을 실행할지. 기본true. 디버깅 목적으로만 끄길 권해요.:profile—:time이면 각 컴파일 사이클 시간을 측정해요.:purge_compiler_modules—true면 컴파일 후 컴파일 모듈을 자동으로 정리해요.:dest— BEAM 파일 대상 디렉터리. 실제로 파일을 쓰려면compile_to_path/3를 써야 해요.:beam_timestamp— 모든 BEAM 파일에 줄 수정 시각.:return_diagnostics(v1.15.0+) — 경고 목록 대신 정보가 담긴 맵을 돌려줘요. 호환성 이유가 아니라면true로 설정해야 해요.:max_concurrency— 병렬로 컴파일할 최대 파일 수.1이면 순차 컴파일. 기본은 온라인 스케줄러 수(최소 2).
compile_to_path/3
@spec compile_to_path([Path.t()], Path.t(), compile_opts()) ::
{:ok, [atom()], [warning()] | info()}
| {:error, [error()] | [Code.diagnostic(:error)], [warning()] | info()}
주어진 파일들을 컴파일하고 결과 BEAM 파일을 path에 써요. 자세한 내용은 compile/2를 참고하세요.
require/2
@spec require([Path.t()], require_opts()) ::
{:ok, [atom()], [warning()] | info()}
| {:error, [error()] | [Code.diagnostic(:error)], [warning()] | info()}
주어진 파일들을 병렬로 require해요. compile과 달리 파일 사이 의존성을 자동으로 해결하려 하지 않아요. require_opts()는 :each_file, :each_module, :max_concurrency, :return_diagnostics를 지원해요.
pmap/2 (v1.16.0+)
collection에 대해 fun으로 병렬 컴파일을 수행해요. 한 파일이 다른 모듈들을 병렬로 컴파일해야 한다면, 새로 뜬 프로세스들이 컴파일러 환경을 알아야 하는데, 이 함수가 그런 작업을 가능하게 해줘요.
async/1 (deprecated)
pmap/2를 대신 사용하세요. 이 함수는 병렬 컴파일을 위한 태스크를 시작했지만 더 이상 권장되지 않아요.
더 알아보기
Code모듈: 컴파일 진단(diagnostic)과 컴파일러 모듈 정리Kernel모듈: 컴파일러가 사용하는 기본 특수 형식과 매크로Mix.Task문서: 실제 빌드 도구가 이 컴파일러를 활용하는 방식