1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/gn/toolchain.h"
9 #include "base/logging.h"
10 #include "tools/gn/target.h"
11 #include "tools/gn/value.h"
13 const char* Toolchain::kToolCc
= "cc";
14 const char* Toolchain::kToolCxx
= "cxx";
15 const char* Toolchain::kToolObjC
= "objc";
16 const char* Toolchain::kToolObjCxx
= "objcxx";
17 const char* Toolchain::kToolRc
= "rc";
18 const char* Toolchain::kToolAsm
= "asm";
19 const char* Toolchain::kToolAlink
= "alink";
20 const char* Toolchain::kToolSolink
= "solink";
21 const char* Toolchain::kToolLink
= "link";
22 const char* Toolchain::kToolStamp
= "stamp";
23 const char* Toolchain::kToolCopy
= "copy";
25 Toolchain::Toolchain(const Settings
* settings
, const Label
& label
)
26 : Item(settings
, label
),
28 setup_complete_(false) {
31 Toolchain::~Toolchain() {
34 Toolchain
* Toolchain::AsToolchain() {
38 const Toolchain
* Toolchain::AsToolchain() const {
43 Toolchain::ToolType
Toolchain::ToolNameToType(const base::StringPiece
& str
) {
44 if (str
== kToolCc
) return TYPE_CC
;
45 if (str
== kToolCxx
) return TYPE_CXX
;
46 if (str
== kToolObjC
) return TYPE_OBJC
;
47 if (str
== kToolObjCxx
) return TYPE_OBJCXX
;
48 if (str
== kToolRc
) return TYPE_RC
;
49 if (str
== kToolAsm
) return TYPE_ASM
;
50 if (str
== kToolAlink
) return TYPE_ALINK
;
51 if (str
== kToolSolink
) return TYPE_SOLINK
;
52 if (str
== kToolLink
) return TYPE_LINK
;
53 if (str
== kToolStamp
) return TYPE_STAMP
;
54 if (str
== kToolCopy
) return TYPE_COPY
;
59 std::string
Toolchain::ToolTypeToName(ToolType type
) {
61 case TYPE_CC
: return kToolCc
;
62 case TYPE_CXX
: return kToolCxx
;
63 case TYPE_OBJC
: return kToolObjC
;
64 case TYPE_OBJCXX
: return kToolObjCxx
;
65 case TYPE_RC
: return kToolRc
;
66 case TYPE_ASM
: return kToolAsm
;
67 case TYPE_ALINK
: return kToolAlink
;
68 case TYPE_SOLINK
: return kToolSolink
;
69 case TYPE_LINK
: return kToolLink
;
70 case TYPE_STAMP
: return kToolStamp
;
71 case TYPE_COPY
: return kToolCopy
;
78 const Tool
* Toolchain::GetTool(ToolType type
) const {
79 DCHECK(type
!= TYPE_NONE
);
80 return tools_
[static_cast<size_t>(type
)].get();
83 void Toolchain::SetTool(ToolType type
, scoped_ptr
<Tool
> t
) {
84 DCHECK(type
!= TYPE_NONE
);
85 DCHECK(!tools_
[type
].get());
87 tools_
[type
] = t
.Pass();
90 void Toolchain::ToolchainSetupComplete() {
91 // Collect required bits from all tools.
92 for (size_t i
= 0; i
< TYPE_NUMTYPES
; i
++) {
94 substitution_bits_
.MergeFrom(tools_
[i
]->substitution_bits());
97 setup_complete_
= true;
101 Toolchain::ToolType
Toolchain::GetToolTypeForSourceType(SourceFileType type
) {
126 const Tool
* Toolchain::GetToolForSourceType(SourceFileType type
) {
127 return tools_
[GetToolTypeForSourceType(type
)].get();
131 Toolchain::ToolType
Toolchain::GetToolTypeForTargetFinalOutput(
132 const Target
* target
) {
133 // The contents of this list might be suprising (i.e. stamp tool for copy
134 // rules). See the header for why.
135 switch (target
->output_type()) {
138 case Target::EXECUTABLE
:
139 return Toolchain::TYPE_LINK
;
140 case Target::SHARED_LIBRARY
:
141 return Toolchain::TYPE_SOLINK
;
142 case Target::STATIC_LIBRARY
:
143 return Toolchain::TYPE_ALINK
;
144 case Target::SOURCE_SET
:
146 case Target::COPY_FILES
:
148 case Target::ACTION_FOREACH
:
152 return Toolchain::TYPE_NONE
;
156 const Tool
* Toolchain::GetToolForTargetFinalOutput(const Target
* target
) const {
157 return tools_
[GetToolTypeForTargetFinalOutput(target
)].get();