Skip to content

Commit 8f7d34b

Browse files
Add tlbimp (#1)
1 parent c3680aa commit 8f7d34b

File tree

12 files changed

+122
-16
lines changed

12 files changed

+122
-16
lines changed

Diff for: README.md

+43-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
# FindIDL [![Build status](https://ci.appveyor.com/api/projects/status/github/apriorit/FindIDL?svg=true)](https://ci.appveyor.com/project/apriorit/findidl)
2-
CMake module for building IDL files with MIDL
2+
CMake module for building IDL files with MIDL and generating CLR DLL using Tlbimp.
33

44
* [Introduction](#introduction)
55
* [Requirements](#requirements)
66
* [Usage](#usage)
77
* [find_package()](#find_package)
88
* [add_idl()](#add_idl)
9+
* [add_idl() with tlbimp](#add_idl-with-tlbimp)
910
* [MIDL flags](#midl-flags)
1011
* [Samples](#samples)
1112
* [License](#license)
1213
* [Version History](#version-history)
1314

1415
# Introduction
15-
IDL is used for creating COM servers. Unfortunately CMake has a limited support for IDL, so this module comes to rescue.
16+
IDL is used for creating COM servers. Unfortunately CMake has a limited support for IDL, so this module comes to rescue. The Type Library Importer (Tlbimp) converts the type definitions found within a COM type library (TLB) into equivalent definitions in a common language runtime assembly. The output of Tlbimp.exe is a binary file (an assembly) that contains runtime metadata for the types defined within the original type library.
1617

1718
## Requirements
1819
- [CMake 3.0](https://cmake.org/download/) or higher
1920
- MIDL compiler
21+
- Tlbimp.exe (optional)
2022

2123
# Usage
2224
## find_package()
@@ -25,10 +27,10 @@ Add [FindIDL](https://github.com/apriorit/FindIDL) to the module search path and
2527
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake")
2628
find_package(IDL REQUIRED)
2729
```
28-
[FindIDL](https://github.com/apriorit/FindIDL) will search for midl.exe
30+
[FindIDL](https://github.com/apriorit/FindIDL) will search for midl.exe and tlbimp.exe
2931

3032
## add_idl()
31-
Takes two arguments: the name of the target project and idl file, possible with full path specified.
33+
Takes two arguments: the name of the target project and idl file.
3234
```cmake
3335
add_idl(<name> source)
3436
```
@@ -41,7 +43,7 @@ Example:
4143
add_idl(GreeterIDL Greeter.idl)
4244
```
4345

44-
The function makes the same work as MIDL, specifically generates files:
46+
The function does the same work as MIDL, specifically generates files:
4547
- Greeter_i.h
4648
- Greeter_i.c
4749
- Greeter_p.c
@@ -52,12 +54,44 @@ To use the generated files the idl project should be linked as following
5254
target_link_libraries(Main GreeterIDL)
5355
```
5456

57+
## add_idl() with tlbimp
58+
Takes four arguments: the name of the target project, idl file, TLBIMP flag and the name of the tlbimp target.
59+
```cmake
60+
add_idl(<name> source TLBIMP <tlbimp name>)
61+
```
62+
Where:
63+
- `<name>` - name of the target project
64+
- `source` - full path to idl file
65+
- `TLBIMP` - flag to indicate to run tlbimp
66+
- `<tlbimp name>` - name of the tlbimp target
67+
68+
Example:
69+
```cmake
70+
add_idl(GreeterIDLWithTLBIMP Greeter.idl TLBIMP GreeterInterop)
71+
```
72+
73+
The function does the same work as MIDL and tlbimp.exe, specifically generates files:
74+
- Greeter_i.h
75+
- Greeter_i.c
76+
- Greeter_p.c
77+
- Greeter.tlb
78+
- GreeterInterop.dll
79+
80+
To use the generated files the idl project should be linked as following
81+
```cmake
82+
target_link_libraries(MainCpp GreeterIDL)
83+
```
84+
Or if you want to use the file generated by tlbimp:
85+
```cmake
86+
target_link_libraries(MainCsharp GreeterInterop)
87+
```
88+
5589
## MIDL flags
5690
It is possible to specify MIDL flags, such as midl command line keys.
5791
```cmake
5892
set(MIDL_FLAGS /target NT60)
5993
```
60-
Current can be useful to avoid MIDL2455 error.
94+
This can be useful to avoid MIDL2455 error.
6195

6296
# Samples
6397
Take a look at the [samples](samples/) folder to see how to use [FindIDL](https://github.com/apriorit/FindIDL).
@@ -67,5 +101,8 @@ Take a look at the [samples](samples/) folder to see how to use [FindIDL](https:
67101

68102
# Version History
69103

104+
## Version 1.0.1 (08 Aug 2019)
105+
- New: Add tlbimp
106+
70107
## Version 1.0.0 (26 Oct 2018)
71108
- Initial public release

Diff for: cmake/FindIDL.cmake

+53-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,58 @@ function(add_idl _target _idlfile)
2121
VERBATIM
2222
)
2323

24-
add_custom_target(${_target}_gen DEPENDS ${MIDL_OUTPUT} SOURCES ${_idlfile})
25-
add_library(${_target} INTERFACE )
26-
add_dependencies(${_target} ${_target}_gen)
24+
set(FINDIDL_TARGET ${_target}_gen)
25+
26+
cmake_parse_arguments(FINDIDL "" "TLBIMP" "" ${ARGN})
27+
28+
if(FINDIDL_TLBIMP)
29+
file(GLOB TLBIMPv7_FILES "C:/Program Files*/Microsoft SDKs/Windows/v7*/bin/TlbImp.exe")
30+
file(GLOB TLBIMPv8_FILES "C:/Program Files*/Microsoft SDKs/Windows/v8*/bin/*/TlbImp.exe")
31+
file(GLOB TLBIMPv10_FILES "C:/Program Files*/Microsoft SDKs/Windows/v10*/bin/*/TlbImp.exe")
32+
33+
list(APPEND TLBIMP_FILES ${TLBIMPv7_FILES} ${TLBIMPv8_FILES} ${TLBIMPv10_FILES})
34+
35+
if(TLBIMP_FILES)
36+
list(GET TLBIMP_FILES -1 TLBIMP_FILE)
37+
endif()
38+
39+
if (NOT TLBIMP_FILE)
40+
message(FATAL_ERROR "Cannot found tlbimp.exe. Try to download .NET Framework SDK and .NET Framework targeting pack.")
41+
return()
42+
endif()
43+
44+
message(STATUS "Found tlbimp.exe: " ${TLBIMP_FILE})
45+
46+
set(TLBIMP_OUTPUT_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
47+
48+
if("${TLBIMP_OUTPUT_PATH}" STREQUAL "")
49+
set(TLBIMP_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
50+
endif()
51+
52+
set(TLBIMP_OUTPUT ${TLBIMP_OUTPUT_PATH}/${FINDIDL_TLBIMP}.dll)
53+
54+
add_custom_command(
55+
OUTPUT ${TLBIMP_OUTPUT}
56+
COMMAND ${TLBIMP_FILE} "${MIDL_OUTPUT_PATH}/${IDL_FILE_NAME_WE}.tlb" "/out:${TLBIMP_OUTPUT}"
57+
DEPENDS ${MIDL_OUTPUT_PATH}/${IDL_FILE_NAME_WE}.tlb
58+
VERBATIM
59+
)
60+
61+
add_custom_target(${FINDIDL_TARGET} DEPENDS ${MIDL_OUTPUT} ${TLBIMP_OUTPUT} SOURCES ${_idlfile})
62+
63+
add_library(${FINDIDL_TLBIMP} SHARED IMPORTED GLOBAL)
64+
add_dependencies(${FINDIDL_TLBIMP} ${FINDIDL_TARGET})
65+
66+
set_target_properties(${FINDIDL_TLBIMP}
67+
PROPERTIES
68+
IMPORTED_LOCATION "${TLBIMP_OUTPUT}"
69+
IMPORTED_COMMON_LANGUAGE_RUNTIME "CSharp"
70+
)
71+
else()
72+
add_custom_target(${FINDIDL_TARGET} DEPENDS ${MIDL_OUTPUT} SOURCES ${_idlfile})
73+
endif()
74+
75+
add_library(${_target} INTERFACE)
76+
add_dependencies(${_target} ${FINDIDL_TARGET})
2777
target_include_directories(${_target} INTERFACE ${MIDL_OUTPUT_PATH})
2878
endfunction()

Diff for: samples/CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
cmake_minimum_required(VERSION 3.0)
2-
3-
project(FindIDL)
1+
cmake_minimum_required(VERSION 3.8)
42

3+
project(FindIDL LANGUAGES C CXX CSharp)
54
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
65

76
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../cmake")

Diff for: samples/Demo/CMakeLists.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
add_subdirectory(GreeterLib)
21
add_subdirectory(GreeterIDL)
3-
add_subdirectory(Main)
2+
add_subdirectory(GreeterIDLWithTLBIMP)
3+
add_subdirectory(GreeterLib)
4+
add_subdirectory(MainCpp)
5+
add_subdirectory(MainCsharp)

Diff for: samples/Demo/GreeterIDL/Greeter.idl

-42 Bytes
Binary file not shown.

Diff for: samples/Demo/GreeterIDLWithTLBIMP/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_idl(GreeterIDLWithTLBIMP Greeter.idl TLBIMP GreeterInterop)

Diff for: samples/Demo/GreeterIDLWithTLBIMP/Greeter.idl

988 Bytes
Binary file not shown.

Diff for: samples/Demo/Main/CMakeLists.txt

-2
This file was deleted.

Diff for: samples/Demo/MainCpp/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(MainCpp Main.cpp)
2+
target_link_libraries(MainCpp GreeterIDL)
File renamed without changes.

Diff for: samples/Demo/MainCsharp/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(MainCsharp Main.cs)
2+
target_link_libraries(MainCsharp GreeterInterop)

Diff for: samples/Demo/MainCsharp/Main.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using GreeterInterop;
3+
4+
namespace Samples
5+
{
6+
class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
GreeterClass obj = new GreeterClass();
11+
obj.greet();
12+
Console.WriteLine("");
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)