small medium large xlarge

01 Mar 2016, 23:43
Tal Lancaster (2 posts)

As others have noted, Google has joined gtest and gmock into one product and have had some problems getting the CMakeLists.txt to build. Here is I wrote a quick and dirty Python script that will recursively go through code tree and update all of the CMakefiles.

I have only tested it as far as chapter 2 and chapter 3.

Also for me, Google had a gmock distribution for OSX which put the files under /usr/local and so I am using /usr/local/lib for the link directory. NOTE Those lines are commented out and so you will need to uncomment them if you want this change, too.

As is here are the three things that are changed: 1. changed c++-x to c++11 2. rename test to tester 3. deletes gtest target to link as it isn’t needed anymore.

Hopefully this makes it ok, as I didn’t see a way to upload a file.

This script will run through and 'update' the CMakeLists.txt files
Provided for the book Modern C++ Programming with Test-Driven Development
from the link:

Author: Tal Lancaster
Although this script has worked for the author, execute this script at your own risk.

The script runs through the source directory tree and tries to make the following changes to the CMakeLists.txt files.  NOTE: The script should be run from just under the 'code' directory.

Version 1.0: 2/29/16
  -- changes c++-x to c++11
  -- renames test to tester (with this change the tests will be built as tester instead of test.
  -- deletes gtest target link library.

import os
import re
import sys

cmakeFile ='CMakeLists.txt'
otherLibPath = "/usr/local/lib"

def updateCmakeList (dir, cmakeFile):
    Change out the contents of the cmake file.

    inFile = os.path.join(dir, cmakeFile)
    with open (inFile, 'r') as fin:
        lines = fin.readlines()

    outFile = os.path.join (dir, 'cMakeNew.txt')
    with open (outFile, 'w') as fout:
        for line in lines:
            if ("target_link_libr.+\w\(\w+ gtest\)", line) ) != None:
                #removing gtest link as no longer needed.
            line = line.replace("std=c++0x", "std=c++11")
            line = line.replace("(test ", "(tester ")

            ## uncomment to add otherLibPath
            ##if (line.find ("link_dir") >= 0 and line.find(otherLibPath) < 0):
            ##   line = line.replace (")", " {})".format(otherLibPath) )


    os.rename(outFile, inFile)

#Assuming in top level example code directory 'code' Modern C++: TDD
for pathDir, dirs, files in os.walk('.'):
    if cmakeFile in files:
        print "Changing path: {} file: {}".format(pathDir, cmakeFile)
        updateCmakeList(pathDir, cmakeFile)

You must be logged in to comment