diff mbox series

[RFC,v3,6/7] block/file-posix: Remove a deprecation warning on macOS 12

Message ID 20220110131001.614319-7-f4bug@amsat.org
State New
Headers show
Series host: Support macOS 12 | expand

Commit Message

Philippe Mathieu-Daudé Jan. 10, 2022, 1:10 p.m. UTC
When building on macOS 12 we get:

  block/file-posix.c:3335:18: warning: 'IOMasterPort' is deprecated: first deprecated in macOS 12.0 [-Wdeprecated-declarations]
      kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
                   ^~~~~~~~~~~~
                   IOMainPort

Replace by IOMainPort, redefining it to IOMasterPort if not
available, using Clang __is_identifier() feature (this code
is guarded by __APPLE__ #ifdef'ry).

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
Checkpatch:

 WARNING: architecture specific defines should be avoided

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 block/file-posix.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Comments

Akihiko Odaki Jan. 10, 2022, 6:28 p.m. UTC | #1
On 2022/01/10 22:10, Philippe Mathieu-Daudé wrote:
> When building on macOS 12 we get:
> 
>    block/file-posix.c:3335:18: warning: 'IOMasterPort' is deprecated: first deprecated in macOS 12.0 [-Wdeprecated-declarations]
>        kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
>                     ^~~~~~~~~~~~
>                     IOMainPort
> 
> Replace by IOMainPort, redefining it to IOMasterPort if not
> available, using Clang __is_identifier() feature (this code
> is guarded by __APPLE__ #ifdef'ry).
> 
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
> Checkpatch:
> 
>   WARNING: architecture specific defines should be avoided
> 
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>   block/file-posix.c | 13 +++++++++----
>   1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/block/file-posix.c b/block/file-posix.c
> index b283093e5b7..1d0512026c5 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -3324,17 +3324,22 @@ BlockDriver bdrv_file = {
>   #if defined(__APPLE__) && defined(__MACH__)
>   static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
>                                   CFIndex maxPathSize, int flags);
> +
> +#if !__is_identifier(IOMainPort) /* macOS >= 12.0 */
> +#define IOMainPort IOMasterPort
> +#endif
> +
>   static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
>   {
>       kern_return_t kernResult = KERN_FAILURE;
> -    mach_port_t     masterPort;
> +    mach_port_t mainPort;
>       CFMutableDictionaryRef  classesToMatch;
>       const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass};
>       char *mediaType = NULL;
>   
> -    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
> +    kernResult = IOMainPort(MACH_PORT_NULL, &mainPort);
>       if ( KERN_SUCCESS != kernResult ) {
> -        printf( "IOMasterPort returned %d\n", kernResult );
> +        printf("IOMainPort returned %d\n", kernResult);
>       }
>   
>       int index;
> @@ -3347,7 +3352,7 @@ static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
>           }
>           CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey),
>                                kCFBooleanTrue);
> -        kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch,
> +        kernResult = IOServiceGetMatchingServices(mainPort, classesToMatch,
>                                                     mediaIterator);
>           if (kernResult != KERN_SUCCESS) {
>               error_report("Note: IOServiceGetMatchingServices returned %d",

Unlike kAudioObjectPropertyElementMain, which is a constant, IOMainPort 
is a symbol, and its availability depends on the runtime. As such, the 
check the existence of the compile-time identifier is not sufficient 
here. MAC_OS_X_VERSION_MIN_REQUIRED should be used instead. (I should 
have suggested the use of MAC_OS_X_VERSION_MIN_REQUIRED at the first 
place since it can be used regardless if the identifier is for a 
constant or a symbol. My Bad.)

Regards,
Akihiko Odaki
diff mbox series

Patch

diff --git a/block/file-posix.c b/block/file-posix.c
index b283093e5b7..1d0512026c5 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -3324,17 +3324,22 @@  BlockDriver bdrv_file = {
 #if defined(__APPLE__) && defined(__MACH__)
 static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
                                 CFIndex maxPathSize, int flags);
+
+#if !__is_identifier(IOMainPort) /* macOS >= 12.0 */
+#define IOMainPort IOMasterPort
+#endif
+
 static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
 {
     kern_return_t kernResult = KERN_FAILURE;
-    mach_port_t     masterPort;
+    mach_port_t mainPort;
     CFMutableDictionaryRef  classesToMatch;
     const char *matching_array[] = {kIODVDMediaClass, kIOCDMediaClass};
     char *mediaType = NULL;
 
-    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
+    kernResult = IOMainPort(MACH_PORT_NULL, &mainPort);
     if ( KERN_SUCCESS != kernResult ) {
-        printf( "IOMasterPort returned %d\n", kernResult );
+        printf("IOMainPort returned %d\n", kernResult);
     }
 
     int index;
@@ -3347,7 +3352,7 @@  static char *FindEjectableOpticalMedia(io_iterator_t *mediaIterator)
         }
         CFDictionarySetValue(classesToMatch, CFSTR(kIOMediaEjectableKey),
                              kCFBooleanTrue);
-        kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch,
+        kernResult = IOServiceGetMatchingServices(mainPort, classesToMatch,
                                                   mediaIterator);
         if (kernResult != KERN_SUCCESS) {
             error_report("Note: IOServiceGetMatchingServices returned %d",