Warning, /sdk/kde-dev-scripts/cmake-utils/scripts/gencmake is written in an unsupported language. File is not indexed.

0001 #!/usr/bin/ruby
0002 
0003 #############################################################################
0004 #    Copyright (C) 2005, 2006 by Tobias Hunger
0005 #    tobias.hunger@basyskom.de
0006 #
0007 #    This program is free software; you can redistribute it and/or modify
0008 #    it under the terms of the GNU General Public License as published by
0009 #    the Free Software Foundation; either version 2 of the License, or
0010 #    (at your option) any later version.
0011 #
0012 #    This program is distributed in the hope that it will be useful,
0013 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015 #    GNU General Public License for more details.
0016 #
0017 #    You should have received a copy of the GNU General Public License
0018 #    along with this program; if not, write to the
0019 #    Free Software Foundation, Inc.,
0020 #    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0021 #############################################################################
0022 
0023 # This script is supposed to build a skeleton cmake configuration file for
0024 # the sources and header files found in the current directory.
0025 #
0026 # It is insprired by "qmake -project" and works with the same limitations.
0027 
0028 require 'fileutils'
0029 
0030 class Project
0031     def initialize()
0032         @pwd = FileUtils.pwd();
0033         @projectName = File.basename(@pwd)
0034         @sources = []
0035         @headers = []
0036         @uis = []
0037         @kcfgs = []
0038         @configs = []
0039 
0040         @got_main = false
0041         @got_cmakelist = false
0042         @got_qt = false
0043         @got_kde = false
0044     end
0045 
0046     def addHeader(header)
0047         @headers.push(header) unless @headers.include?(header)
0048     end
0049 
0050     def haveHeaders?
0051         ! @headers.empty?
0052     end
0053 
0054     def addSource(source)
0055         @sources.push(source) unless @sources.include?(source)
0056     end
0057 
0058     def haveSources?
0059         ! @sources.empty?
0060     end
0061 
0062     def addUi(ui)
0063         @got_qt = true
0064         @uis.push(ui) unless @uis.include?(ui)
0065     end
0066 
0067     def haveUis?
0068         ! @uis.empty?
0069     end
0070 
0071     def addKcfg(kcfg)
0072         @got_kde = true
0073         @kcfgs.push(kcfg) unless @kcfgs.include?(kcfg)
0074     end
0075 
0076     def haveKcfgs?
0077         ! @kcfgs.empty?
0078     end
0079 
0080     def addConfig(config)
0081         @configs.push(config) unless @configs.include?(config)
0082     end
0083 
0084     def haveConfigs?
0085         ! @configs.empty?
0086     end
0087 
0088     attr_reader :pwd, :projectName, :headers, :sources, :uis, :kcfgs, :configs,
0089                 :got_main, :got_cmakelist, :got_qt, :got_kde
0090     attr_writer :pwd, :projectName, :got_main, :got_cmakelist, :got_qt, :got_kde
0091 end
0092 
0093 class Scanner
0094     def initialize(project)
0095         @project = project
0096     end
0097 
0098     def findFiles()
0099         Dir.entries(@project.pwd).each do |file|
0100             @project.got_cmakelist = true if /^CMakeLists.txt$/.match(File.basename(file))
0101             @project.addHeader(file) if /(.h|.hh|.H|.hpp|.hxx)$/.match(File.basename(file))
0102             @project.addSource(file) if /(.c|.cc|.C|.cpp|.cxx)$/.match(File.basename(file))
0103             @project.addUi(file) if /(.ui)$/.match(File.basename(file))
0104             @project.addKcfg(file) if /(.kcfgc)$/.match(File.basename(file))
0105             @project.addConfig(file) if /(.in)$/.match(File.basename(file))
0106         end
0107     end
0108 
0109     def scanFile(file)
0110         last_line = "";
0111         File.foreach(file) do |line|
0112             # empty lines:
0113             next if /^\s*$/.match(line)
0114 
0115             # scan for includes
0116             if /^\s*#\s*include\s*[<"](.*)[>"]\s*$/.match(line)
0117                 @project.got_qt = true if /^Q(Debug|List|String|Hash|Application)$/.match($1)
0118                 @project.got_kde = true if /^k(debug|kio\/|application)$/.match($1)
0119             end
0120 
0121             # do we have a main function?
0122             @project.got_main = true if /^(void|int|)\s*main\s*/.match(line)
0123         end
0124     end
0125 
0126     def scanFiles()
0127         @project.headers.each { |i| scanFile(i); }
0128         @project.sources.each { |i| scanFile(i); }
0129     end
0130 end
0131 
0132 class Generator
0133     def initialize(project)
0134         @project = project
0135         @install_location = ""
0136         @target_type = "executable"
0137         @target_type = "library" unless @project.got_main
0138         @prefix = "#{@project.projectName}_#{@target_type}"
0139     end
0140 
0141     def header()
0142         "# ########## Project setup ##########\n" +
0143         "PROJECT(#{@project.projectName})\n" +
0144         "CMAKE_MINIMUM_REQUIRED(VERSION 2.4.5)\n\n"
0145     end
0146 
0147     def setup()
0148         "# ######### General setup ##########\n" +
0149         "INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})\n\n"
0150     end
0151 
0152     def files()
0153         output = "# ########## #{@project.projectName} #{@target_type} ##########\n" +
0154         "# Sources:\n" +
0155         "SET(#{@prefix}_SRCS\n"
0156         @project.sources.each { |i| output+= "    #{i}\n" }
0157         output += ")\n\n" +
0158         "# Headers:\n" +
0159         "SET(#{@prefix}_HDRS\n"
0160         @project.headers.each { |i| output += "    #{i}\n" }
0161         output += ")\n\n"
0162         output
0163     end
0164 
0165     def specials()
0166         ""
0167     end
0168 
0169     def configfiles()
0170         output = ""
0171         @project.configs.each do |i|
0172             output += "CONFIGURE_FILE(#{i} ${CMAKE_BINARY_DIR}/#{i[0..-4]})\n"
0173         end
0174         output += "\n" unless output == ""
0175         return output
0176     end
0177 
0178     def target()
0179         output = "# actual target:\n"
0180         if @project.got_main
0181             @install_location = "bin"
0182             output += "ADD_EXECUTABLE(#{@project.projectName} ${#{@prefix}_SRCS})\n"
0183         else
0184             @install_location = "lib"
0185             output += "ADD_LIBRARY(#{@project.projectName} ${#{@prefix}}_SCRS ${#{@prefix}_HDRS})\n" +
0186             "SET_TARGET_PROPERTIES(#{@project.projectName} PROPERTIES VERSION 0.0.0)\n"
0187         end
0188         return output
0189     end
0190 
0191     def libraries()
0192         ""
0193     end
0194 
0195     def install()
0196         "# add install target:\n" +
0197         "INSTALL(TARGETS #{@project.projectName} DESTINATION #{@install_location})"
0198     end
0199 
0200     def generate()
0201         File.open("CMakeLists.txt", "w") do |file|
0202             file.puts header
0203             file.puts setup
0204 
0205             file.puts files
0206             file.puts configfiles
0207             file.puts specials
0208 
0209             file.puts target
0210             file.puts libraries
0211             file.puts install
0212         end
0213     end
0214 end
0215 
0216 
0217 class QtGenerator < Generator
0218     def initialize(project)
0219         super(project)
0220     end
0221 
0222     def setup
0223         "# ########## Qt4 setup ##########\n" +
0224         "FIND_PACKAGE(Qt4 REQUIRED)\n" +
0225         "INCLUDE(${QT_USE_FILE})\n" +
0226         "INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${QT_INCLUDES})\n\n"
0227     end
0228 
0229     def files()
0230         output = super
0231         if @project.haveUis?
0232             output += "# UI files:\n" +
0233             "SET(#{@prefix}_UIS\n"
0234             @project.uis.each { |i| output += "    #{i}\n" }
0235             output += ")\n\n"
0236         end
0237         output
0238     end
0239 
0240     def specials()
0241         "# scan files and run moc on all that need it:\n" +
0242         "QT4_AUTOMOC(${#{@prefix}_SRCS})\n\n"
0243     end
0244 
0245     def libraries()
0246         "TARGET_LINK_LIBRARIES(#{@project.projectName} ${QT_LIBRARIES})\n\n"
0247     end
0248 end
0249 
0250 class KDEGenerator < QtGenerator
0251     def initialize(project)
0252         super(project)
0253     end
0254 
0255     def setup
0256         "# ########## KDE4 setup ##########\n" +
0257         "FIND_PACKAGE(KDE4 REQUIRED)\n" +
0258         "INCLUDE(KDE4Defaults)\n" +
0259         "INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${KDE4_INCLUDE_DIR} ${QT_INCLUDES})\n\n"
0260     end
0261 
0262     def files()
0263         output = super()
0264         if @project.haveKcfgs?
0265             output += "# KCFG files:\n" +
0266             "SET(#{@prefix}_KCFGS\n"
0267             @project.kcfgs.each { |i| output += "    #{i}\n" }
0268             output += ")\n\n"
0269         end
0270         output
0271     end
0272 
0273     def specials()
0274         output = "# Special code for KDE4:\n"
0275         output += "# " unless @project.haveUis?
0276         output += "KDE4_ADD_UI_FILES(${#{@prefix}_SRCS} ${#{@prefix}_UIS})\n"
0277         output += "# " unless @project.haveKcfgs?
0278         output += "KDE4_ADD_KCFG_FILES(${#{@prefix}_SRCS} ${#{@prefix}_KCFGS})\n\n"
0279     end
0280 
0281     def target()
0282         "# actual target:\n" +
0283         "KDE4_ADD_EXECUTABLE(#{@project.projectName} ${#{@prefix}_SRCS} ${#{@qprefix}_HDRS})\n"
0284     end
0285 
0286     def libraries()
0287         "TARGET_LINK_LIBRARIES(#{@project.projectName} ${KDE4_KDEUI_LIBS})\n\n"
0288     end
0289 
0290     def install()
0291         "# add install target:\n" +
0292         "INSTALL(TARGETS #{@project.projectName} DESTINATION ${BIN_INSTALL_DIR})"
0293     end
0294 
0295 end
0296 
0297 
0298 my_project = Project.new
0299 my_scanner = Scanner.new(my_project)
0300 
0301 my_scanner.findFiles()
0302 
0303 my_scanner.scanFiles()
0304 
0305 if my_project.got_kde
0306     my_generator = KDEGenerator.new(my_project)
0307 elsif my_project.got_qt
0308     my_generator = QtGenerator.new(my_project)
0309 else
0310     my_generator = Generator.new(my_project)
0311 end
0312 my_generator.generate