Skip to content

Instantly share code, notes, and snippets.

@tim37021
Created February 2, 2020 12:47
Show Gist options
  • Save tim37021/4a4756bced2dd0c462b5cef2d22d8372 to your computer and use it in GitHub Desktop.
Save tim37021/4a4756bced2dd0c462b5cef2d22d8372 to your computer and use it in GitHub Desktop.
Dump include path of cmake cache
# license MIT
# Hsu Yu Lun
import subprocess
import json
import time
def start(executable_file):
return subprocess.Popen(
executable_file,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
def readline(process):
return process.stdout.readline().decode("utf-8").strip()
def read_response(process):
process.stdout.readline().decode("utf-8").strip()
process.stdout.readline().decode("utf-8").strip()
res = process.stdout.readline().decode("utf-8").strip()
data = json.loads(res)
process.stdout.readline().decode("utf-8").strip()
return data
def write(process, message):
process.stdin.write(message.encode("utf-8"))
process.stdin.flush()
def terminate(process):
process.stdin.close()
process.terminate()
process.wait(timeout=0.2)
def handshake(process, major, minor):
request = """[== "CMake Server" ==[
{"cookie":"zimtstern","type":"handshake","protocolVersion":{"major":%d, "minor": %d},
"sourceDirectory":"C:/Users/tim/Desktop/Test", "buildDirectory":"C:/Users/tim/Desktop/Test/build",
"generator":"Ninja"}
]== "CMake Server" ==]
"""%(major, minor)
write(process, request)
return read_response(process)
def simple_command(process, cmd):
request = """[== "CMake Server" ==[
{"type":"%s"}
]== "CMake Server" ==]
"""%(cmd)
write(process, request)
return read_response(process)
def cache(process):
return simple_command(process, "cache")
def configure(process):
response = simple_command(process, "configure")
while True:
if response['type']=='reply':
break
response = read_response(process)
return response
#print(read_response(process))
def compute(process):
response = simple_command(process, "compute")
while True:
if response['type']=='reply':
break
response = read_response(process)
return response
def codemodel(process):
return simple_command(process, "codemodel")
process = start("cmake -E server --debug --experimental")
protocal = read_response(process)
protocal_major = protocal['supportedProtocolVersions'][0]['major']
protocal_minor = protocal['supportedProtocolVersions'][0]['minor']
handshake(process, protocal_major, protocal_minor)
cache(process)
configure(process)
compute(process)
time.sleep(0.1)
codemodel = codemodel(process)
path_dict = codemodel['configurations'][0]['projects'][0]['targets'][0]['fileGroups'][0]['includePath']
for d in path_dict:
print("\"%s\","%d['path'])
terminate(process)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment