forked from pavelliavonau/cmakeconverter
/
projectfiles.py
124 lines (112 loc) · 4.84 KB
/
projectfiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2016-2017:
# Matthieu Estrada, ttamalfor@gmail.com
#
# This file is part of (CMakeConverter).
#
# (CMakeConverter) is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# (CMakeConverter) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with (CMakeConverter). If not, see <http://www.gnu.org/licenses/>.
from message import send
class ProjectFiles(object):
c_folder_nb = 1
h_folder_nb = 1
def __init__(self, data):
self.tree = data['vcxproj']['tree']
self.ns = data['vcxproj']['ns']
self.cmake = data['cmake']
self.cppfiles = self.tree.xpath('//ns:ClCompile', namespaces=self.ns)
self.headerfiles = self.tree.xpath('//ns:ClInclude', namespaces=self.ns)
# TODO : put this method in projectvariables file
def write_variables(self):
# Cpp Dir
known_cpp = []
ProjectFiles.c_folder_nb = 1
self.cmake.write('# Folders files\n')
for cpp in self.cppfiles:
if cpp.get('Include') is not None:
cxx = str(cpp.get('Include'))
current_cpp = '/'.join(cxx.split('\\')[0:-1])
if current_cpp not in known_cpp:
known_cpp.append(current_cpp)
self.cmake.write(
'set(CPP_DIR_%s %s)\n' % (str(ProjectFiles.c_folder_nb), current_cpp)
)
ProjectFiles.c_folder_nb += 1
# Headers Dir
known_headers = []
ProjectFiles.h_folder_nb = 1
for header in self.headerfiles:
h = str(header.get('Include'))
current_header = '/'.join(h.split('\\')[0:-1])
if current_header not in known_headers:
known_headers.append(current_header)
self.cmake.write(
'set(HEADER_DIR_%s %s)\n' % (str(ProjectFiles.h_folder_nb), current_header)
)
ProjectFiles.h_folder_nb += 1
def write_files(self):
"""
Add directory variables to SRC_FILES
"""
# Add files to project
# TODO Glob Recurse for files.
self.cmake.write('################ Files ################\n'
'# -- Add files to project. -- #\n'
'#######################################\n\n')
self.cmake.write('file(GLOB SRC_FILES\n')
c = 1
while c < ProjectFiles.c_folder_nb:
self.cmake.write(' ${CPP_DIR_' + str(c) + '}/*.cpp\n')
c += 1
h = 1
while h < ProjectFiles.h_folder_nb:
self.cmake.write(' ${HEADER_DIR_' + str(h) + '}/*.h\n')
h += 1
self.cmake.write(')\n\n')
def add_additional_code(self, file_to_add):
if file_to_add != '':
try:
fc = open(file_to_add, 'r')
self.cmake.write('############# Additional Code #############\n')
self.cmake.write('# Provides from external file. #\n')
self.cmake.write('###########################################\n\n')
for line in fc:
self.cmake.write(line)
fc.close()
self.cmake.write('\n')
send('File of Code is added = ' + file_to_add, 'warn')
except OSError:
send(
'Wrong data file ! Code was not added, please verify file name or path !',
'error'
)
def add_artefact(self):
"""
Add Libraries and Executables
"""
configurationtype = self.tree.find('//ns:ConfigurationType', namespaces=self.ns)
if configurationtype.text == 'DynamicLibrary':
self.cmake.write('# Add library to build.\n')
self.cmake.write('add_library(${PROJECT_NAME} SHARED\n')
send('CMake will build a SHARED Library.', '')
elif configurationtype.text == 'StaticLibrary':
self.cmake.write('# Add library to build.\n')
self.cmake.write('add_library(${PROJECT_NAME} STATIC\n')
send('CMake will build a STATIC Library.', '')
else:
self.cmake.write('# Add executable to build.\n')
self.cmake.write('add_executable(${PROJECT_NAME} \n')
send('CMake will build an EXECUTABLE.', '')
self.cmake.write(' ${SRC_FILES}\n')
self.cmake.write(')\n\n')