CMake and REGEX REPLACE question

1,679 views
Skip to first unread message

NoAntzWk

unread,
Feb 8, 2011, 10:21:55 AM2/8/11
to firebre...@googlegroups.com
Hi,

I want to process manifest.json file, exactly I want to remove javascripts comments from a file using CMake.

---[ manifest.json ]---
//This is a cmake generated file from a template located @ */plugins/firebreath/projects/dummy/Win/Helpers/Templates
// Chrome manifest template
// 

{
"name": "Dummy Plugin",
"version": "1.0.0.1",
"minimum_chrome_version": "6.0",
"plugins":
[
{ "path": "Plugins/npdummy.dll", "public": true },
]
}
---[ manifest.json ]---

In my projectDef.cmake looks like:
---[ projectDef.cmake ]---
# ...
configure_file( # Needed on Build_CRX process.
Win/Helpers/Templates/manifest.json
${CMAKE_CURRENT_SOURCE_DIR}/Win/Helpers/Build_CRX
)
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/Win/Helpers/Build_CRX/manifest.json file_content)
message("Input:\n${file_content}")
set (file_content_res)
string(REGEX REPLACE "(^(//)[^\n]*)" "" file_content_res "${file_content}")
string(REGEX REPLACE "(^[\n])" "" file_content_res "${file_content_res}")
message("Output:\n${file_content_res}")
# file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/Win/Helpers/Build_CRX/manifest.json" "${file_content}")
# ...
---[ projectDef.cmake ]---

But this CMake code only removes first line and doesn't remove all lines that start with "//". What's wrong? I expect that "${file_content_res}" has all manifest file without all comment lines :-?

Thanks in advance.

Best regards

Richard Bateman

unread,
Feb 8, 2011, 10:37:49 AM2/8/11
to firebre...@googlegroups.com

Wow.  Tricky stuff; the best example I can point you to is the configure_template function I wrote that does some similarish stuff: https://github.com/firebreath/FireBreath/blob/master/cmake/configure_template.cmake

Basically the things to know about cmake string processing is that it's weird and fraught with frustration :-/ CMake lists are actually stored as strings with ";" seperating the items, so if your source string has any ; in it they can get changed into multiple line entries.  configure_template does some search/replace to try to work around that, but I think it still discards blank lines at this point.

Honestly I'd try to avoid doing what you're doing with cmake if you don't absolutely have to, but sometimes you gotta do what you gotta do =]

hope that helps,

Richard

Luigi Calori

unread,
Feb 8, 2011, 10:49:19 AM2/8/11
to firebre...@googlegroups.com
Not really sure, but maybe you can try to use

file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/Win/Helpers/Build_CRX/manifest.json file_content_lines)
foreach(file_content ${
file_content_lines}
   ----- do your stuff for each line ----
   string(REGEX REPLACE "(^(//)[^\n]*)" "" file_content_res "${file_content}")
   string(REGEX REPLACE "(^[\n])" "" file_content_res "${file_content_res}")
   message("Output:\n${file_content_res}")
   file(APPEND ${CMAKE_CURRENT_SOURCE_DIR}/Win/Helpers/Build_CRX/manifest.json "${file_content_res}")
endforeach()

HTH

NoAntzWk

unread,
Feb 9, 2011, 6:14:23 AM2/9/11
to firebre...@googlegroups.com
Hi Luigi,

Interesting... it seems that could work. But not really for the manifest.json file provided. The problem is character "[" in manifest.json file when retrieve file using "file(STRINGS...". I have tried the next code, but doesn't work:

file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/Win/Helpers/Build_CRX/manifest.json file_content_lines)
string(REGEX REPLACE "[" "\\\\[" file_content_lines "${file_content_lines}")
foreach(file_content ${
file_content_lines
   # ----- do your stuff for each line ----
   string(REGEX REPLACE "(^(//)[^\n]*)" "" file_content_res "${file_content}")
   string(REGEX REPLACE "(^[\n])" "" file_content_res "${file_content_res}")
   string
(REGEX REPLACE ";" "\n" file_content_res "${file_content_res }")
   string
(REGEX REPLACE "\\\\;" ";" file_content_res "${file_content_res}")
   message("Output:\n${file_content_res}")

   file(APPEND ${CMAKE_CURRENT_SOURCE_DIR}/Win/Helpers/Build_CRX/manifest.json "${file_content_res}")
endforeach()

Thank you, for your comments.

Reply all
Reply to author
Forward
0 new messages