1#!/usr/bin/env python
2
3################################################################################
4##
5## Photivo
6##
7## Copyright (C) 2013 Sergey Salnikov <salsergey@gmail.com>
8##
9## This file is part of Photivo.
10##
11## Photivo is free software: you can redistribute it and/or modify
12## it under the terms of the GNU General Public License version 3
13## as published by the Free Software Foundation.
14##
15## Photivo is distributed in the hope that it will be useful,
16## but WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Photivo.  If not, see <http://www.gnu.org/licenses/>.
22##
23################################################################################
24#
25# This script generates CMakeLists.txt file. The script extracts all
26# sources, headers and UIs from the photivoProject/photivoProject.pro
27# file and adds them to CMakeLists.txt.in.
28#
29################################################################################
30
31import sys
32import os.path
33import re
34
35# Function to find if the source should be added.
36def test_source(filename):
37  if filename.endswith('cpp') and not re.match('.*qtlockedfile.*', filename):
38    return True
39  else:
40    return False
41
42# Function to find if the header file should be MOCed.
43def test_header(filename):
44  file = open(filename)
45  for line in file:
46    if re.match('.*Q_OBJECT.*', line):
47      return True
48  return False
49
50# Function that extracts the path to a file.
51# The returned value means if the file list continues.
52def match_to_path(files, line, test_function=None):
53  if line.endswith('\\'):
54    result = True
55  else:
56    result = False
57  if not re.match('^#', line):
58    line = re.split('\\\$', line)[0].strip()
59    if re.match('.*\.\./', line):
60      line = re.split('\.\./', line)[1]
61      if test_function == None or test_function(line):
62        files.append(line)
63  return result
64
65
66# set the working directory to that containing this script
67os.chdir(os.path.dirname(sys.argv[0]))
68
69if not os.path.exists('CMakeLists.txt.in'):
70  print 'File CMakeLists.txt.in doesn\'t exist.'
71  exit(1)
72
73if not os.path.exists('photivoProject/photivoProject.pro'):
74  print 'File photivoProject/photivoProject.pro doesn\'t exist.'
75  exit(1)
76
77cmake_in = open('CMakeLists.txt.in', 'r')
78qmake_pro = open('photivoProject/photivoProject.pro', 'r')
79cmake_out = open('CMakeLists.txt', 'w')
80
81sources = []
82headers = []
83uis = []
84skip = False
85copy_src = False
86copy_hdr = False
87copy_ui = False
88
89
90for line in qmake_pro:
91  line = line.strip()
92# these lines correspond to win32 only and we skip them
93  if re.match('win32', line):
94    skip = True
95# the end of the win32 section
96  if re.match('}', line):
97    skip = False
98  if skip:
99    continue
100
101# sources section found
102  if re.match('SOURCES', line):
103    copy_src = True
104  if copy_src:
105    copy_src = match_to_path(sources, line, test_source)
106    continue
107
108# headers section found
109  if re.match('HEADERS', line):
110    copy_hdr = True
111  if copy_hdr:
112    copy_hdr = match_to_path(headers, line, test_header)
113    continue
114
115# forms section found
116  if re.match('FORMS', line):
117    copy_ui = True
118  if copy_ui:
119    copy_ui = match_to_path(uis, line)
120    continue
121
122
123for line in cmake_in:
124  cmake_out.write(line)
125
126# sources section found
127  if re.match('^set\( photivo_SRCS', line):
128    cmake_out.write('     ' + '\n     '.join(sources))
129
130# headers section found
131  if re.match('^set\( photivo_MOC_HDRS', line):
132    cmake_out.write('     ' + '\n     '.join(headers))
133
134# forms section found
135  if re.match('^set\( photivo_UI_HDRS', line):
136    cmake_out.write('     ' + '\n     '.join(uis))
137
138
139cmake_in.close()
140qmake_pro.close()
141cmake_out.close()
142