1 | //----------------------------------------------------------- |
---|
2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
3 | // arnault@lal.in2p3.fr |
---|
4 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
5 | //----------------------------------------------------------- |
---|
6 | |
---|
7 | #include "cmt_system.h" |
---|
8 | #include "cmt.h" |
---|
9 | #include "cmt_install_area.h" |
---|
10 | #include "cmt_syntax.h" |
---|
11 | #include "cmt_use.h" |
---|
12 | #include "cmt_symbol.h" |
---|
13 | #include "cmt_cmtpath_pattern.h" |
---|
14 | #include "cmt_project.h" |
---|
15 | |
---|
16 | CmtInstallAreaMgr& CmtInstallAreaMgr::instance () |
---|
17 | { |
---|
18 | static CmtInstallAreaMgr me; |
---|
19 | |
---|
20 | return (me); |
---|
21 | } |
---|
22 | |
---|
23 | void CmtInstallAreaMgr::setup_current_installarea () |
---|
24 | { |
---|
25 | m_installarea = build_current_installarea (); |
---|
26 | |
---|
27 | if (m_installarea == "") return; |
---|
28 | |
---|
29 | cmt_string installarea_prefix; |
---|
30 | |
---|
31 | Symbol* macro = Symbol::find ("cmt_installarea_prefix"); |
---|
32 | if (macro != 0) |
---|
33 | { |
---|
34 | installarea_prefix = macro->build_macro_value (); |
---|
35 | } |
---|
36 | |
---|
37 | if (installarea_prefix != "") |
---|
38 | { |
---|
39 | m_installarea += CmtSystem::file_separator (); |
---|
40 | m_installarea += installarea_prefix; |
---|
41 | } |
---|
42 | |
---|
43 | Use& current_use = Use::current (); |
---|
44 | cmt_string buffer; |
---|
45 | |
---|
46 | if (!Symbol::is_selected ("CMTINSTALLAREA")) |
---|
47 | { |
---|
48 | buffer = "macro CMTINSTALLAREA \""; |
---|
49 | buffer += m_installarea; |
---|
50 | buffer += "\""; |
---|
51 | |
---|
52 | SyntaxParser::parse_requirements_line (buffer, ¤t_use); |
---|
53 | buffer = ""; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | void CmtInstallAreaMgr::setup () |
---|
58 | { |
---|
59 | m_installarea = build_current_installarea (); |
---|
60 | |
---|
61 | setup_current_installarea (); |
---|
62 | } |
---|
63 | |
---|
64 | void CmtInstallAreaMgr::config () const |
---|
65 | { |
---|
66 | cmt_string installarea = build_current_installarea (); |
---|
67 | |
---|
68 | CmtPathPattern::apply_all (); |
---|
69 | |
---|
70 | if (installarea == "") return; |
---|
71 | |
---|
72 | // cout << "InstallArea installed in " << installarea << endl; |
---|
73 | |
---|
74 | if (!Cmt::get_quiet ()) |
---|
75 | { |
---|
76 | cerr << "#CMT> Warning: Doing cleanup in the installation area " << installarea << endl; |
---|
77 | } |
---|
78 | |
---|
79 | Symbol* macro = Symbol::find ("cmt_installarea_paths"); |
---|
80 | if (macro == 0) return; |
---|
81 | cmt_string installarea_paths = macro->build_macro_value (); |
---|
82 | |
---|
83 | CmtSystem::cmt_string_vector areapaths; |
---|
84 | CmtSystem::split (installarea_paths, " \t", areapaths); |
---|
85 | |
---|
86 | // Try a cleanup only in this selected install area |
---|
87 | |
---|
88 | for (int i = 0; i < areapaths.size (); i++) |
---|
89 | { |
---|
90 | const cmt_string& p = areapaths[i]; |
---|
91 | |
---|
92 | cmt_string path = installarea; |
---|
93 | path += CmtSystem::file_separator (); |
---|
94 | path += p; |
---|
95 | |
---|
96 | Symbol::expand (path); |
---|
97 | |
---|
98 | path.replace_all ("/", CmtSystem::file_separator ()); |
---|
99 | path.replace_all ("\\", CmtSystem::file_separator ()); |
---|
100 | |
---|
101 | CmtSystem::cmt_string_vector refs; |
---|
102 | cmt_regexp expression (".*[.]cmtref$"); |
---|
103 | |
---|
104 | // Look for all cmtref files in this PATH pattern |
---|
105 | |
---|
106 | CmtSystem::scan_dir (path, expression, refs); |
---|
107 | |
---|
108 | for (int j = 0; j < refs.size (); j++) |
---|
109 | { |
---|
110 | const cmt_string& ref_file = refs[j]; |
---|
111 | |
---|
112 | // We get the absolute location of the installed file |
---|
113 | |
---|
114 | cmt_string ref; |
---|
115 | ref.read (ref_file); |
---|
116 | int pos; |
---|
117 | |
---|
118 | ref.replace_all ("\"", ""); |
---|
119 | |
---|
120 | if (Cmt::get_debug ()) |
---|
121 | { |
---|
122 | cout << "CmtInstallAreaMgr::config> " << ref_file << " " << ref << endl; |
---|
123 | } |
---|
124 | |
---|
125 | pos = ref.find ("\r\n"); |
---|
126 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
127 | pos = ref.find ('\n'); |
---|
128 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
129 | pos = ref.find ('\r'); |
---|
130 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
131 | pos = ref.find (' '); |
---|
132 | if (pos != cmt_string::npos) ref.erase (pos); |
---|
133 | |
---|
134 | // If the referenced file cannot be reached we remove the |
---|
135 | // corresponding installation |
---|
136 | // (this happens if the referenced file has be removed, or |
---|
137 | // moved away) |
---|
138 | |
---|
139 | if (!CmtSystem::test_file (ref)) |
---|
140 | { |
---|
141 | cmt_string ref_name; |
---|
142 | |
---|
143 | // Get the name of the referenced file |
---|
144 | CmtSystem::basename (ref, ref_name); |
---|
145 | |
---|
146 | // Get the installation directory |
---|
147 | CmtSystem::dirname (ref_file, ref); |
---|
148 | |
---|
149 | ref += CmtSystem::file_separator (); |
---|
150 | ref += ref_name; |
---|
151 | |
---|
152 | // Remove both the installed file |
---|
153 | // and the reference file |
---|
154 | |
---|
155 | if (!Cmt::get_quiet ()) |
---|
156 | { |
---|
157 | cout << "# Removing obsolete installed file [" << ref << "]"; |
---|
158 | cout << " (and " << ref_file << ")" << endl; |
---|
159 | } |
---|
160 | |
---|
161 | CmtSystem::remove_file (ref); |
---|
162 | CmtSystem::remove_file (ref_file); |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | const cmt_string& CmtInstallAreaMgr::get_installarea () const |
---|
169 | { |
---|
170 | return (m_installarea); |
---|
171 | } |
---|
172 | |
---|
173 | cmt_string CmtInstallAreaMgr::build_current_installarea () const |
---|
174 | { |
---|
175 | cmt_string installarea; |
---|
176 | |
---|
177 | const cmt_string pwd = CmtSystem::pwd (); |
---|
178 | |
---|
179 | installarea = Project::find_in_cmt_paths (pwd); |
---|
180 | |
---|
181 | return (installarea); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | |
---|